【发布时间】: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