【问题标题】:transition of second-hand in a CSS analog clock from sec==59 to 0CSS 模拟时钟中的秒针从 sec==59 转换为 0
【发布时间】:2022-11-04 00:32:37
【问题描述】:

我希望将二手从 sec==59 平稳过渡到 sec==0。

它从 0 到 59 秒进展顺利,但在 sec==59 上它不会前进而是回滚到 0 位置。我正在尝试顺时针方向从 59 平滑过渡到 0(秒针、分针和时针相同)。这是时钟的完整代码 -

let secHand = document.querySelector(".sec-hand")
let minHand = document.querySelector(".min-hand")
let hourHand = document.querySelector(".hour-hand")
let hands = document.querySelectorAll(".hand")

function setTime() {
  const now = new Date();
  const sec = now.getSeconds();
  const secDegrees = ((sec / 60) * 360) + 90;
  secHand.style.transform = `rotate(${secDegrees}deg)`;

  const mint = now.getMinutes();
  const mintDegrees = ((mint / 60) * 360) + 90;
  minHand.style.transform = `rotate(${mintDegrees}deg)`;

  const hour = now.getHours();
  const hourDegrees = ((hour / 12) * 360) + 90;
  hourHand.style.transform = `rotate(${hourDegrees}deg)`;

  //console.log(`${hour} : ${mint} : ${sec}`)
}
setInterval(setTime, 1000)
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.clock {
  background: transparent;
  width: 400px;
  height: 400px;
  margin: auto;
  margin-top: 100px;
  border-radius: 50%;
  border: 20px solid blue;
  position: absolute;
  left: 200px;
}

.center-dot {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: rgb(195, 4, 4);
  position: absolute;
  top: calc(50% - 25px);
  left: calc(50% - 25px);
  z-index: 5;
}

.hand {
  width: 45%;
  height: 10px;
  background: black;
  position: absolute;
  top: calc(50% - 5px);
  left: 5%;
  border-radius: 10px;
  transform-origin: right;
  transition: all 0.5s;
  transform: rotate(90deg)
}
<div class="clock">
  <div class="center-dot"></div>
  <div class="hour-hand hand"></div>
  <div class="min-hand hand"></div>
  <div class="sec-hand hand"></div>
</div>

我正在关注一个关于这个的视频,在那个视频中,导师提到了两个解决方案 - 首先是在 sec=59 之后继续计数,而不是回到 sec=0 (但我不喜欢这个解决方案)。 其次是通过 JS 在 sec==59 上临时删除转换。我不知道该怎么做。我在setTime() 中尝试过,但没有奏效-

if(sec == 59) {
        hands[2].setAttribute("transition", "");
    }

请帮忙。 谢谢!

【问题讨论】:

    标签: javascript frontend transition


    【解决方案1】:

    你需要记录secCount, mintCount, hourCount

    let secHand = document.querySelector('.sec-hand') ;
    let minHand = document.querySelector('.min-hand') ;
    let hourHand = document.querySelector('.hour-hand') ;
    let hands = document.querySelectorAll('.hand');
    
    
    const delay = async (n = 0) => new Promise((res) => setTimeout(res, n));
    
    (async () => {
      let secCount = 0;
      let mintCount = 0;
      let hourCount = 0;
      for (;;) {
        const now = new Date();
        const sec = now.getSeconds();
        const mint = now.getMinutes();
        const hour = now.getHours();
        if (sec == 0) {
          secCount++;
          if (mint == 0) {
            mintCount++;
            if (hour == 0) {
              hourCount++;
            }
          }
        }
        const secDegrees = (sec / 60 + secCount) * 360 + 90;
        secHand.style.transform = `rotate(${secDegrees}deg)`;
    
        const mintDegrees = (mint / 60 + mintCount) * 360 + 90;
        minHand.style.transform = `rotate(${mintDegrees}deg)`;
    
        const hourDegrees = (hour / 12 + hourCount) * 360 + 90;
        hourHand.style.transform = `rotate(${hourDegrees}deg)`;
        await delay(1000);
      }
    })();
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    .clock {
      background: transparent;
      width: 400px;
      height: 400px;
      margin: auto;
      margin-top: 100px;
      border-radius: 50%;
      border: 20px solid blue;
      position: absolute;
      left: 200px;
    }
    
    .center-dot {
      width: 50px;
      height: 50px;
      border-radius: 50%;
      background: rgb(195, 4, 4);
      position: absolute;
      top: calc(50% - 25px);
      left: calc(50% - 25px);
      z-index: 5;
    }
    
    .hand {
      width: 45%;
      height: 10px;
      background: black;
      position: absolute;
      top: calc(50% - 5px);
      left: 5%;
      border-radius: 10px;
      transform-origin: right;
      transition: all 0.5s;
      transform: rotate(90deg)
    }
    <div class="clock">
      <div class="center-dot"></div>
      <div class="hour-hand hand"></div>
      <div class="min-hand hand"></div>
      <div class="sec-hand hand"></div>
    </div>

    【讨论】:

    • 正是我想要的样子,但我不明白它的工作原理,因为我不熟悉 async 和 promise。我需要了解他们。非常感谢。
    【解决方案2】:

    无关:(几乎)仅限 CSS。

    您只需使用 JS 设置一次当前时间。

    document.body.style.setProperty("--time", Math.floor(Date.now() / 1000  - new Date().getTimezoneOffset() * 60) % (60 * 60 * 24));
    
    @keyframes turn {
      from {
        transform: rotate(90deg);
      }
      to {
        transform: rotate(450deg);
      }
    }
    
    .sec-hand {
      /*animation: turn 60s infinite steps(60);*/
      animation: turn 60s infinite linear;
      animation-delay: calc(var(--time) * -1s);
    }
    
    .min-hand {
      /*animation: turn 3600s infinite steps(60);*/
      animation: turn 3600s infinite linear;
      animation-delay: calc(var(--time) * -1s);
    }
    
    .hour-hand {
      /*animation: turn 43200s infinite steps(12);*/
      animation: turn 43200s infinite linear;
      animation-delay: calc(var(--time) * -1s);
    }
    

    缺点:缺乏关于缓动功能的选择。 您可以让时钟手柄连续运行,或步进(每秒/分钟/小时一次),但步骤之间没有缓动。

    document.body.style.setProperty("--time", Math.floor(Date.now() / 1000  - new Date().getTimezoneOffset() * 60) % (60 * 60 * 24));
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    .clock {
      background: transparent;
      width: 400px;
      height: 400px;
      margin: auto;
      margin-top: 100px;
      border-radius: 50%;
      border: 20px solid blue;
      position: absolute;
      left: 200px;
    }
    
    .center-dot {
      width: 50px;
      height: 50px;
      border-radius: 50%;
      background: rgb(195, 4, 4);
      position: absolute;
      top: calc(50% - 25px);
      left: calc(50% - 25px);
      z-index: 5;
    }
    
    .hand {
      width: 45%;
      height: 10px;
      background: black;
      position: absolute;
      top: calc(50% - 5px);
      left: 5%;
      border-radius: 10px;
      transform-origin: right;
    }
    
    
    
    
    @keyframes turn {
      from {
        transform: rotate(90deg)
      }
      to {
        transform: rotate(450deg)
      }
    }
    
    .sec-hand {
      --animation: turn 60s infinite steps(60);
      animation: turn 60s infinite linear;
      animation-delay: calc(var(--time) * -1s);
    }
    
    .min-hand {
      --animation: turn 3600s infinite steps(60);
      animation: turn 3600s infinite linear;
      animation-delay: calc(var(--time) * -1s);
    }
    
    .hour-hand {
      --animation: turn 43200s infinite steps(12);
      animation: turn 43200s infinite linear;
      animation-delay: calc(var(--time) * -1s);
    }
    <div class="clock">
      <div class="center-dot"></div>
      <div class="hour-hand hand"></div>
      <div class="min-hand hand"></div>
      <div class="sec-hand hand"></div>
    </div>

    【讨论】:

    • 谢谢!我真的很喜欢像你一样应用 CSS 动画。确实有你提到的缺点。你让它很容易理解。
    【解决方案3】:

    您可能需要做更多的工作来平滑过渡,但设置过渡,正如您所尝试的那样,CSS 不是 HTML attribute

    let secHand = document.querySelector(".sec-hand")
    let minHand = document.querySelector(".min-hand")
    let hourHand = document.querySelector(".hour-hand")
    let hands = document.querySelectorAll(".hand")
    
    function setTime() {
      const now = new Date();
      const sec = now.getSeconds();
      if (sec === 59) {
        secHand.style.transition = "0.5s";
      } else {
        secHand.style.transition = "0s";
      }
      const secDegrees = ((sec / 60) * 360) + 90;
      secHand.style.transform = `rotate(${secDegrees}deg)`;
    
      const mint = now.getMinutes();
      const mintDegrees = ((mint / 60) * 360) + 90;
      minHand.style.transform = `rotate(${mintDegrees}deg)`;
    
      const hour = now.getHours();
      const hourDegrees = ((hour / 12) * 360) + 90;
      hourHand.style.transform = `rotate(${hourDegrees}deg)`;
    
      //console.log(`${hour} : ${mint} : ${sec}`)
    }
    setInterval(setTime, 1000)
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    .clock {
      background: transparent;
      width: 400px;
      height: 400px;
      margin: auto;
      margin-top: 100px;
      border-radius: 50%;
      border: 20px solid blue;
      position: absolute;
      left: 200px;
    }
    
    .center-dot {
      width: 50px;
      height: 50px;
      border-radius: 50%;
      background: rgb(195, 4, 4);
      position: absolute;
      top: calc(50% - 25px);
      left: calc(50% - 25px);
      z-index: 5;
    }
    
    .hand {
      width: 45%;
      height: 10px;
      background: black;
      position: absolute;
      top: calc(50% - 5px);
      left: 5%;
      border-radius: 10px;
      transform-origin: right;
      transition: all 0.5s;
      transform: rotate(90deg)
    }
    <div class="clock">
      <div class="center-dot"></div>
      <div class="hour-hand hand"></div>
      <div class="min-hand hand"></div>
      <div class="sec-hand hand"></div>
    </div>

    【讨论】:

    • 谢谢!真的行。今天通过这个答案学到了一些新东西。
    • 没问题,只需确保在几分钟和几小时内执行相同的逻辑
    猜你喜欢
    • 1970-01-01
    • 2013-01-12
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 2015-05-18
    相关资源
    最近更新 更多