亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

如何讓時(shí)鐘上的秒針只會(huì)向前走動(dòng)?
P粉952365143
P粉952365143 2023-09-06 15:56:38
0
1
777

我使用 React 建立了一個(gè)類比時(shí)鐘,但秒針有問題。 秒針以順時(shí)針方向開始,但一旦到達(dá) 59 秒標(biāo)記,它就會(huì)逆時(shí)針移動(dòng),直到到達(dá) 1 秒標(biāo)記。之後它又順時(shí)針移動(dòng)。如何讓它連續(xù)順時(shí)針移動(dòng)?

import React, { useEffect, useState } from 'react';
import './AnalogClock.css';

const AnalogClock = () => {
  const [time, setTime] = useState(new Date());

  useEffect(() => {
    const interval = setInterval(() => {
      const newTime = new Date();
      setTime(newTime);
    }, 1000);

    return () => {
      clearInterval(interval);
    };
  }, []);

  const getRotation = (unit, max) => {
    const value = time[`get${unit}`]();
    let rotation = (value * 360) / max;

    if (unit === 'Seconds') {
      const seconds = value + time.getMilliseconds() / 1000;
      rotation = (seconds * 6) % 360;

      if (rotation < 0) {
        rotation += 360;
      }
    }

    return {
      transform: `translate(-50%, -100%) rotate(${rotation}deg)`,
    };
  };

  const renderNumbers = () => {
    const numbers = [];

    for (let i = 1; i <= 12; i++) {
      const angle = (i * 30) * (Math.PI / 180);
      const numberStyle = {
        left: `calc(50% + ${Math.sin(angle) * 140}px)`,
        top: `calc(50% - ${Math.cos(angle) * 140}px)`,
      };
      numbers.push(
        <div key={i} className="number" style={numberStyle}>
          {i}
        </div>
      );
    }

    return numbers;
  };

  return (
    <div className="clock">
      {renderNumbers()}
      <div className="hour-hand" style={getRotation('Hours', 12)}></div>
      <div className="minute-hand" style={getRotation('Minutes', 60)}></div>
      <div
        className="second-hand"
        style={
          time.getSeconds() === 59
            ? { ...getRotation('Seconds', 60), transition: 'none' }
            : getRotation('Seconds', 60)
        }
      ></div>
      <div className="center-circle"></div>
    </div>
  );
};

export default AnalogClock;

不確定 css 是否有幫助,但它就在這裡。

.clock {
    position: relative;
    width: 300px;
    height: 300px;
    border-radius: 50%;
    background-color: #e0e0e0;
    box-shadow: 20px 20px 40px rgba(0, 0, 0, 0.2), -20px -20px 40px rgba(255, 255, 255, 0.7);
    padding: 20px;
  }
  
  .hour-hand,
  .minute-hand,
  .second-hand {
    position: absolute;
    background-color: #333;
    transform-origin: bottom center;
    transition: transform 0.5s ease-in-out;
    box-shadow: 4px 4px 8px rgba(0, 0, 0, 0.2), -4px -4px 8px rgba(255, 255, 255, 0.7);
  }
  
  .hour-hand {
    width: 8px;
    height: 90px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -100%) translateX(-1px);
    border-radius: 8px 8px 0 0;
  }
  
  .minute-hand {
    width: 6px;
    height: 120px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -100%) translateX(-1px);
    border-radius: 6px 6px 0 0;
  }
  
  .second-hand {
    width: 4px;
    height: 130px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -100%) translateX(-1px);
    border-radius: 4px 4px 0 0;
    background-color: #ff0000;
  }
  
  .center-circle {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 16px;
    height: 16px;
    background-color: #333;
    border-radius: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2), -2px -2px 4px rgba(255, 255, 255, 0.7);
  }
  .number {
    position: absolute;
    font-size: 29px;
    font-weight: bold;
    color: #333;
    transform: translate(-50%, -50%);
  }

P粉952365143
P粉952365143

全部回覆(1)
P粉221046425

當(dāng)你嘗試在 css 中註入 js 來實(shí)現(xiàn)你的目標(biāo)時(shí),我建議你使用樣式化元件來獲得一些好處,將其提升到一個(gè)新的水平,並在 css 中使用 js 變數(shù)

程式碼的主要限制是您無法指定動(dòng)畫將永遠(yuǎn)繼續(xù),以便在達(dá)到 360 度時(shí)重新啟動(dòng)動(dòng)畫

這可以透過 css 關(guān)鍵影格和動(dòng)畫來實(shí)現(xiàn),但如果不使用樣式化元件,則很難將一些必要的參數(shù)傳遞給關(guān)鍵影格和動(dòng)畫,例如初始度數(shù)和完成迴圈的秒數(shù)

這或多或少是樣式化元件的方法

import React, { useEffect, useState } from 'react';
import styled, { keyframes } from 'styled-components';
import './styles.css';

const rotate = (degree) => keyframes`
  from {
    transform: translate(-50%, -100%) rotate(${degree}deg);
  }
  to {
    transform: translate(-50%, -100%) rotate(${degree+360}deg);
  }
`

const ClockStick = styled.div`
  animation: ${(props) => rotate(props.degree)} ${(props) => props.seconds}s linear;
`

const App = () => {
  const [degrees, setDegrees] = useState({});

  useEffect(() => {
    setDegrees({
      hour: getRotation('Hours', 12),
      minute: getRotation('Minutes', 60),
      second: getRotation('Seconds', 60)
    })
  }, []);

  const getRotation = (unit, max) => {
    const time = new Date();
    const value = time[`get${unit}`]();
    const rotation = (value * 360) / max;
    return rotation;
  };

  const renderNumbers = () => {
    const numbers = [];

    for (let i = 1; i <= 12; i++) {
      const angle = (i * 30) * (Math.PI / 180);
      const numberStyle = {
        left: `calc(50% + ${Math.sin(angle) * 140}px)`,
        top: `calc(50% - ${Math.cos(angle) * 140}px)`,
      };
      numbers.push(
        <div key={i} className="number" style={numberStyle}>
          {i}
        </div>
      );
    }

    return numbers;
  };

  return (
    <div className="clock">
      {renderNumbers()}
      <ClockStick className="hour-hand" seconds={216000} degree={degrees.hour}></ClockStick>
      <ClockStick className="minute-hand" seconds={3600} degree={degrees.minute}></ClockStick>
      <ClockStick
        className="second-hand"
        seconds={60}
        degree={degrees.second}
      ></ClockStick>
      <div className="center-circle"></div>
    </div>
  );
};

export default App;

我刪除了一些不必要的邏輯,我將解釋我添加的一些內(nèi)容

  • 關(guān)鍵影格對(duì)於實(shí)現(xiàn)此目標(biāo)至關(guān)重要,因?yàn)樗鼘⒍x動(dòng)畫如何旋轉(zhuǎn)(從初始旋轉(zhuǎn)到初始旋轉(zhuǎn) 360),並且還可以與動(dòng)畫屬性一起使用,這對(duì) ClockStick 很有幫助
  • ClockStick 是一個(gè)樣式元件,它將接收初始度數(shù)以及動(dòng)畫完成一個(gè)循環(huán)所需的秒數(shù)
  • 我僅聲明了一種狀態(tài),其初始度數(shù)與用戶運(yùn)行應(yīng)用程式的特定時(shí)間相對(duì)應(yīng)
  • 然後,我傳遞每根棒 (ClockStick) 完成循環(huán)所需的秒數(shù)(216000 => 表示小時(shí),3600 => 表示分鐘,60 => 表示秒)

這是結(jié)果

https://codesandbox.io/s/dawn-rgb-qn58t6

與答案相關(guān)的一些連結(jié)

#
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板