紧接着昨天的实例,第二个是原生js实现钟表特效。
首先介绍下大体思路,首先要用css把时针分针和秒针画出来。而后根据钟表中,角度和时间的算法关系。css
设置角度。git
最后使用定时器,每秒运行一次。github
须要注意的是,个人算法和以前的算法不同,这个能够根据本身的想法实现,实现的效果是不同的。算法
首先知道钟表是360°,而后根据一个小时30°,来算出各个针的角度。spa
https://github.com/CookaCooki... 附上gayhub地址code
<script> const secondHand = document.querySelector('.second-hand'); const minsHand = document.querySelector('.min-hand'); const hourHand = document.querySelector('.hour-hand'); function setDate() { const now = new Date(); const seconds = now.getSeconds(); const secondsDegrees = ((seconds / 60) * 360) + 90; secondHand.style.transform = `rotate(${secondsDegrees}deg)`; const mins = now.getMinutes(); const minsDegrees = ((mins / 60) * 360) + ((seconds / 60) * 6) + 90; minsHand.style.transform = `rotate(${minsDegrees}deg)`; const hour = now.getHours(); const hourDegrees = ((hour / 12) * 360) + ((mins / 60) * 30) + 90; hourHand.style.transform = `rotate(${hourDegrees}deg)`; } setInterval(setDate, 1000); setDate(); </script>
最后符上知乎地址 https://zhuanlan.zhihu.com/p/...orm