时钟几乎家家墙上都会挂一个,也是大家比较常见的物件了。今天就通过HTNL+CSS+JAVASCRIPT来现实一个动态时钟的效果。
思路
1.先通过HTML+CSS在页面画一个静态的时钟。css方面涉及到了css3伪类(before,after)、transform(translate,rotate),这方面欠缺的可自行百度下,这里就不细将了
2.绘制过程包括:表盘、刻度、时针、分针、秒针、中轴
3.获取到时、分、秒分别对应的DIV
4.获取到系统时间对应的时、分、秒的值
5.通过时间对应的时、分、秒的值来修改对应的时、分、秒DIV的旋转角度
6.通过setInterval()来动态的修改对应的时、分、秒DIV的旋转角度
HTML静态效果图
主要HTML代码
<div class="box">
<div class="line">
<div></div>//刻度
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div class="h"></div>//时
<div class="m"></div>//分
<div class="s"></div>//秒
</div>
css代码部分
*{padding: 0px;margin: 0px;}
body{background: #34495e;width: 100vw;height: 100vh;display: flex;justify-content: center;align-items: center;}
.box{width: 400px;height: 400px;background: linear-gradient(135deg, #FDD819, #E80505,#FB2828);border-radius: 50%;
cursor: pointer;position: relative;box-shadow: 0px 0px 15px rgba(255,116,116,.2);}
.box::before{width: 90%;height: 90%;background: #34495E;border-radius: 50%;position: absolute;left: 50%;top:50%;transform: translate(-50%,-50%);
content: '';}
.line{width: 80%;height: 80%;position: relative;top: 50%;left: 50%;border-radius: 50%;transform: translate(-50%,-50%);}
.line div{width: 2px;height: 100%;background: #fff;position: absolute;left: 50%;}
.line div:nth-child(1){transform: rotate(0deg) translate(-50%);}
.line div:nth-child(2){transform: rotate(30deg) translate(-50%);}
.line div:nth-child(3){transform: rotate(60deg) translate(-50%);}
.line div:nth-child(4){transform: rotate(90deg) translate(-50%);}
.line div:nth-child(5){transform: rotate(120deg) translate(-50%);}
.line div:nth-child(6){transform: rotate(150deg) translate(-50%);}
.line::before{width: 90%;height: 90%;background: #34495E;border-radius: 50%;position: absolute;left: 50%;top:50%;transform: translate(-50%,-50%);
content: '';z-index: 2;}
.line::after{width: 20px;height: 20px;background: #fff;border-radius: 50%;position: absolute;left: 50%;top:50%;transform: translate(-50%,-50%);
content: '';z-index: 2;}
.h{width: 6px;height: 20%;background: #fff;position: absolute;left: 50%;bottom: 50%; transform: translateX(-50%) rotate(45deg);
transform-origin: bottom;}
.m{width: 4px;height: 28%;background: #fff;position: absolute;left: 50%;bottom: 50%; transform: translateX(-50%) rotate(25deg);
transform-origin: bottom;}
.s{width: 2px;height: 35%;background: #fff;position: absolute;left: 50%;bottom: 50%; transform: translateX(-50%) rotate(65deg);
transform-origin: bottom;}
js部分
1.获取到时、分、秒对应的div
function $q(a){//系统函数自定义一下,为了方便
return document.querySelector(a)
}
let h = $q('.h')
let m = $q('.m')
let s = $q('.s')
2.获取当前时间对应的时、分、秒,其中说明下,getHours()获取到的是24进制的小时数,所以对12进行取余了一下,得到12进制的小时数
let time = new Date()
let time_hs = time.getHours()
let time_h = time_hs%12
let time_m = time.getMinutes()
let time_s = time.getSeconds()
3.根据时间得到的时、分、秒的值来修改对应的时、分、秒DIV的css样式(旋转的角度);
3.1:小时数为0-12小时,一圈为360度,那么每个小时对应的是30度
3.2:分钟数为0-60分,一圈为360度,那么每分钟对应的为6度
3.3:秒数为0-60秒,一圈为360度,那么每秒对应的为6度
3.4:我们知道分针每转1分钟的时候,时针也是会动的,那么分针每转1分钟的时候,时针会增加360/12/60=0.5度,就比如:3点40的时候,这个时候时针不是在3点的刻度上,而是在3点-4点之间的刻度上,也就是时针要多旋转40x0.5=20度;如果以0点为界限,3点40分的时候,时针的位置应该指向表盘刻度:3x30+40x0.5=110度的位置上。
h.style['transform'] = 'translateX(-50%) rotate('+(time_h*30+time_m*0.5)+'deg)'
m.style['transform'] = 'translateX(-50%) rotate('+(time_m*6)+'deg)'
s.style['transform'] = 'translateX(-50%) rotate('+(time_s*6)+'deg)'
4.将上述的js部分封装在自定义函数里,然后通过setInterval()来动态的获得时、分、秒DIV旋转角度而得到的位置
全部代码
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>js-动态时钟</title>
</head>
<style>
*{padding: 0px;margin: 0px;}
body{background: #34495e;width: 100vw;height: 100vh;display: flex;justify-content: center;align-items: center;}
.box{width: 400px;height: 400px;background: linear-gradient(135deg, #FDD819, #E80505,#FB2828);border-radius: 50%;
cursor: pointer;position: relative;box-shadow: 0px 0px 15px rgba(255,116,116,.2);}
.box::before{width: 90%;height: 90%;background: #34495E;border-radius: 50%;position: absolute;left: 50%;top:50%;transform: translate(-50%,-50%);
content: '';}
.line{width: 80%;height: 80%;position: relative;top: 50%;left: 50%;border-radius: 50%;transform: translate(-50%,-50%);}
.line div{width: 2px;height: 100%;background: #fff;position: absolute;left: 50%;}
.line div:nth-child(1){transform: rotate(0deg) translate(-50%);}
.line div:nth-child(2){transform: rotate(30deg) translate(-50%);}
.line div:nth-child(3){transform: rotate(60deg) translate(-50%);}
.line div:nth-child(4){transform: rotate(90deg) translate(-50%);}
.line div:nth-child(5){transform: rotate(120deg) translate(-50%);}
.line div:nth-child(6){transform: rotate(150deg) translate(-50%);}
.line::before{width: 90%;height: 90%;background: #34495E;border-radius: 50%;position: absolute;left: 50%;top:50%;transform: translate(-50%,-50%);
content: '';z-index: 2;}
.line::after{width: 20px;height: 20px;background: #fff;border-radius: 50%;position: absolute;left: 50%;top:50%;transform: translate(-50%,-50%);
content: '';z-index: 2;}
.h{width: 6px;height: 20%;background: #fff;position: absolute;left: 50%;bottom: 50%; transform: translateX(-50%) rotate(45deg);
transform-origin: bottom;}
.m{width: 4px;height: 28%;background: #fff;position: absolute;left: 50%;bottom: 50%; transform: translateX(-50%) rotate(25deg);
transform-origin: bottom;}
.s{width: 2px;height: 35%;background: #fff;position: absolute;left: 50%;bottom: 50%; transform: translateX(-50%) rotate(65deg);
transform-origin: bottom;}
</style>
<body>
<div class="box">
<div class="line">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div class="h"></div>
<div class="m"></div>
<div class="s"></div>
</div>
</body>
<script type="text/javascript">
function $q(a){
return document.querySelector(a)
}
times()
function times(){
let h = $q('.h')
let m = $q('.m')
let s = $q('.s')
let time = new Date()
let time_hs = time.getHours()
let time_h = time_hs%12
let time_m = time.getMinutes()
let time_s = time.getSeconds()
h.style['transform'] = 'translateX(-50%) rotate('+(time_h*30+time_m*0.5)+'deg)'
m.style['transform'] = 'translateX(-50%) rotate('+(time_m*6)+'deg)'
s.style['transform'] = 'translateX(-50%) rotate('+(time_s*6)+'deg)'
}
setInterval(times,1000)
</script>
</html>
作者
本文作者:武汉盈科动力网络科技有限公司-武汉做网站小编