利用定时器实现数字时钟
<!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 type="text/css">
*{margin: 0px;padding: 0px;}
#clock{
width: 600px;
height: 600px;
background: url("images/clock.jpg") no-repeat;
position: relative;
}
#hour,#minute,#second{
position: absolute;
width: 30px;
height: 600px;
left: 50%;
top: 0;
margin-left: -15px;
}
#hour{
background: url("images/hour.png") no-repeat center center;
}
#minute{
background: url("images/minute.png") no-repeat center center;
}
#second{
background: url("images/second.png") no-repeat center center;
}
</style>
</head>
<body>
<div id="clock">
<div id="hour"></div>
<div id="minute"></div>
<div id="second"></div>
</div>
<script>
var hour=document.getElementById('hour');
var minute=document.getElementById('minute');
var second=document.getElementById('second');
setInterval(function(){
var now=new Date();
var s=now.getSeconds();
var m=now.getMinutes()+s/60;
var h=now.getHours()%12+m/60;
second.style.transform=`rotate(${s*6}deg)`;
minute.style.transform=`rotate(${m*6}deg)`;
hour.style.transform=`rotate(${h*30}deg)`;
},10);
</script>
</body>
</html>