动态时钟 通过计算三个时间针的角度 使用rotate()来调整他们的角度,水平角度为0deg 然后开一个定时器动起来 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .clock { width: 300px; height: 300px; border-radius: 50%; border: 10px solid darkgray; background-color: lightgray; margin: 0 auto; position: relative; } .mark { width: 10px; height: 10px; border-radius: 50%; background-color: darkgray; position: absolute; } .twe { left: 145px; top: 10px; } .thr { right: 10px; top: 145px; } .six { bottom:10px; left: 145px; } .nine { left: 10px; top: 145px; } .center { width: 20px; height: 20px; border-radius: 50%; background-color: darkgray; position: absolute; top: 140px; left: 140px; } .hand { position: absolute; left: 150px; border-radius: 0 50% 50% 0; transform-origin: 0; } .hour { width: 100px; height: 10px; background-color: red; top: 145px; } .min { width: 115px; height: 8px; background-color: blue; top: 146px; } .sec { width: 140px; height: 6px; background-color: rgb(22, 21, 21); top: 147px; } .weekday { width: 50px; height: 20px; position: absolute; right: 20px; top: 140px; } .weekday span { margin: 3px; background-color: antiquewhite; } .time{ width: 66px; height: 20px; bottom: 25px; left: 117px; background-color: antiquewhite; position: absolute; } </style></head><body> <div class="clock"> <div class="mark twe"></div> <div class="mark thr"></div> <div class="mark six"></div> <div class="mark nine"></div> <div class="hand hour"></div> <div class="hand min"></div> <div class="hand sec"></div> <div class="weekday"> <span>05</span><span>日</span> </div> <div class="time">23:59:59</div> <div class="center"></div> </div> <script> var hands = document.getElementsByClassName("hand"); var weekday = document.getElementsByClassName("weekday")[0]; var dayspan = weekday.getElementsByTagName("span")[0]; var weekspan = weekday.getElementsByTagName("span")[1]; var time = document.getElementsByClassName("time")[0]; var clock = function() { var timeNow = new Date(); var day = timeNow.getDate(); var week = timeNow.getDay(); var weeks = ['日','一','二','三','四','五','六'] dayspan.innerHTML = format(day); weekspan.innerHTML = weeks[week]; var h = timeNow.getHours(); var m = timeNow.getMinutes(); var s = timeNow.getSeconds(); time.innerHTML = format(h) + ":" + format(m) + ":" + format(s); var sRot = s * 6 - 90; var mRot = m * 6 + (s/10) - 90; var hRot = h * 30 + (m / 2) - 90; // hands[2].style.transform = `rotate(${sRot}deg)`; hands[2].style.transform ="rotate(" + sRot + "deg)"; hands[1].style.transform ="rotate(" + mRot + "deg)"; hands[0].style.transform = "rotate(" + hRot + "deg)"; } // clock(); setInterval(clock,1000) function format(n) { return n>9?n:('0'+n); } </script></body></html>