<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>钟表特效</title>
<style>
.clock {
width: 600px;
height: 600px;
margin: 0 auto;
background: url(images/clock.jpg) no-repeat;
position: relative;
}
.clock div {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
}
.h {
background: url(images/hour.png) no-repeat center center;
}
.m {
background: url(images/minute.png) no-repeat center center;
}
.s {
background: url(images/second.png) no-repeat center center;
}
</style>
</head>
<body>
<div class="clock">
<div class="h" id="hour"></div>
<div class="m" id="minute"></div>
<div class="s" id="second"></div>
</div>
</body>
</html>
<script>
var hour = document.getElementById("hour");
var minute = document.getElementById("minute");
var second = document.getElementById("second");
// 定义变量保存 秒、分、时、毫秒
var s = 0, m = 0, h = 0, ms = 0;
// 开始定时器
setInterval(function () {
var time = new Date();
// 得到最新的时间
var t = time.getHours()+time.getMinutes()+time.getSeconds()+time.getMilliseconds();
h = time.getHours() + time.getMinutes()/60;
m = time.getMinutes() + time.getSeconds()/60;
s = time.getSeconds() ;
// ms = time.getMilliseconds();
// 计算并设置旋转角度
// 一圈 360 ° 一共有 60 秒 每秒 6 ° 现在是 s秒
second.style.WebkitTransform = "rotate(" + s * 6 + "deg)";
// 变化旋转deg度
minute.style.WebkitTransform = "rotate(" + m * 6 + "deg)";
hour.style.WebkitTransform = "rotate(" + h * 30 + "deg)";
second.style.MozTransform = "rotate(" + s * 6 + "deg)";
// 变化旋转de度
minute.style.MozTransform = "rotate(" + m * 6 + "deg)";
hour.style.MozTransform = "rotate(" + h * 30 + "deg)";
}, 100);
</script>
图片素材
时针分针秒针