<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>时钟</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.box{
width: 600px;
height: 600px;
margin: 300px auto;
background: url(img/clock.jpg) no-repeat;
position: relative;
}
.box .h,.m,.s{
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
text-align: center;
}
.box .h{
background: url(img/hour.png) no-repeat center center;
}
.box .m{
background: url(img/minute.png) no-repeat center center;
}
.box .s{
background: url(img/second.png) no-repeat center center;
}
</style>
</head>
<body>
<div id="box" class="box">
<div class="h" id="hour"></div>
<div class="m" id="minutes"></div>
<div class="s" id="seconds"></div>
</div>
</body>
<script type="text/javascript">
var h = 0,m = 0,s = 0, ms = 0;
setInterval(function(){
var hour = document.getElementById("hour");
var minutes = document.getElementById("minutes");
var seconds = document.getElementById("seconds");
var date = new Date(); // 得到的是最新时间
ms = date.getMilliseconds(); // 得到毫秒数
s = date.getSeconds() + ms / 1000; // 得到秒
m = date.getMinutes() + s / 60; // 得到分
h = date.getHours() % 12 + m / 60; // 得到 小时
//console.log(h);
// 旋转角度 一圈360度 每秒旋转6 度 每分钟旋转 6 度 每小时 旋转30 度
seconds.style.WebkitTransform = "rotate("+s*6+"deg)";
minutes.style.WebkitTransform = "rotate("+m*6+"deg)";
hour.style.WebkitTransform = "rotate("+h*30+"deg)";
seconds.style.MozTransform = "rotate("+s*6+"deg)";
minutes.style.MozTransform = "rotate("+m*6+"deg)";
hour.style.MozTransform = "rotate("+h*30+"deg)";
},1000);
</script>
</html>
效果: