-
Notifications
You must be signed in to change notification settings - Fork 0
/
时钟案例.html
110 lines (92 loc) · 2.52 KB
/
时钟案例.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<!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>
* {
margin: 0;
padding: 0;
}
ul {
list-style: none;
}
#wrap {
width: 400px;
margin: 200px auto;
/*border: 1px solid red;*/
user-select: none;
}
#wrap ul {
display: flex;
}
#wrap li {
margin: 0 3px;
}
#wrap .time {
/*background-color: pink;*/
transition: top 0.3s;
}
#wrap li:not(.dot) {
width: 100%;
height: 62px;
background: linear-gradient(to bottom, #434343 50%, #393939 50%);
border-radius: 10px;
line-height: 62px;
text-align: center;
color: #fff;
font-size: 26px;
}
#wrap .dot {
width: 20px;
height: 62px;
line-height: 62px;
text-align: center;
font-size: 26px;
font-weight: bolder;
}
</style>
</head>
<body>
<div id="wrap">
<ul>
<li class="hours">0</li>
<li class="hours">0</li>
<li class="dot">:</li>
<li class="minutes">0</li>
<li class="minutes">0</li>
<li class="dot">:</li>
<li class="seconds">0</li>
<li class="seconds">0</li>
</ul>
</div>
<script>
let aLi = document.querySelectorAll(" #wrap li:not(.dot)")
//先执行一次,防止页面刷新的瞬间出现全0的情况
fn()
setInterval(fn, 1000)
function fn() {
let date = new Date()
//获取new出的时间的小时,分钟,秒
let h = date.getHours()
let m = date.getMinutes()
let s = date.getSeconds()
//将小时,分钟,秒全部用函数变为两位
h = toTwo(h)
m = toTwo(m)
s = toTwo(s)
//拼接字符串,str就是要显示的结果
let str = h + m + s
//循环遍历 渲染页面
for (let i = 0; i < aLi.length; i++) {
aLi[i].innerHTML = str[i]
}
}
//将传入的值变为两位数
function toTwo(num) {
return (num < 10 ? "0" : "") + num
}
</script>
</body>
</html>