Skip to content

Commit 176760c

Browse files
committed
add
1 parent 2961e14 commit 176760c

File tree

3 files changed

+345
-0
lines changed

3 files changed

+345
-0
lines changed

纯js时间罗盘/index.css

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
* {
2+
margin: 0;
3+
padding: 0;
4+
}
5+
6+
body {
7+
background-color: #000;
8+
}
9+
10+
#box {
11+
width: 800px;
12+
height: 800px;
13+
position: fixed;
14+
top: 0px;
15+
right: 0px;
16+
left: 0px;
17+
bottom: 0px;
18+
margin: auto;
19+
}
20+
21+
.secondBox,
22+
.branchBox,
23+
.hourBox,
24+
.NumberBox,
25+
.monthBox {
26+
border-radius: 50%;
27+
position: absolute;
28+
top: 0px;
29+
right: 0px;
30+
left: 0px;
31+
bottom: 0px;
32+
margin: auto;
33+
}
34+
35+
.second,
36+
.branch,
37+
.hour,
38+
.Number,
39+
.month {
40+
color: #fff;
41+
height: 20px;
42+
font-size: 15px;
43+
position: absolute;
44+
top: 0px;
45+
left: 0px;
46+
right: 0px;
47+
bottom: 0px;
48+
margin: auto;
49+
display: flex;
50+
justify-content: space-between;
51+
}
52+
53+
/* 秒 */
54+
.secondBox {
55+
width: 800px;
56+
height: 800px;
57+
animation-name: secondBox;
58+
animation-duration: 1.4s;
59+
animation-timing-function: linear;
60+
}
61+
62+
@keyframes secondBox {
63+
0% {
64+
transform: rotate(0deg);
65+
}
66+
67+
100% {
68+
transform: rotate(300deg);
69+
}
70+
}
71+
72+
.second {
73+
width: 800px;
74+
}
75+
76+
/* 分钟 */
77+
.branchBox {
78+
width: 650px;
79+
height: 650px;
80+
animation-name: branchBox;
81+
animation-duration: 1.4s;
82+
animation-timing-function: linear;
83+
}
84+
85+
@keyframes branchBox {
86+
0% {
87+
transform: rotate(0deg);
88+
}
89+
90+
100% {
91+
transform: rotate(-310deg);
92+
}
93+
}
94+
95+
.branch {
96+
width: 650px;
97+
}
98+
99+
/* 小时 */
100+
.hourBox {
101+
width: 500px;
102+
height: 500px;
103+
animation-name: hourBox;
104+
animation-duration: 1.4s;
105+
animation-timing-function: linear;
106+
}
107+
108+
@keyframes hourBox {
109+
0% {
110+
transform: rotate(0deg);
111+
}
112+
113+
100% {
114+
transform: rotate(310deg);
115+
}
116+
}
117+
118+
.hour {
119+
width: 500px;
120+
}
121+
122+
/* 日 盒子 */
123+
.NumberBox {
124+
width: 350px;
125+
height: 350px;
126+
animation-name: NumberBox;
127+
animation-duration: 1.4s;
128+
animation-timing-function: linear;
129+
}
130+
131+
@keyframes NumberBox {
132+
0% {
133+
transform: rotate(0deg);
134+
}
135+
136+
100% {
137+
transform: rotate(-120deg);
138+
}
139+
}
140+
141+
.Number {
142+
width: 350px;
143+
}
144+
145+
/* 月份盒子 */
146+
.monthBox {
147+
width: 200px;
148+
height: 200px;
149+
animation-name: monthBox;
150+
animation-duration: 1.4s;
151+
animation-timing-function: linear;
152+
}
153+
154+
@keyframes monthBox {
155+
0% {
156+
transform: rotate(0deg);
157+
}
158+
159+
100% {
160+
transform: rotate(180deg);
161+
}
162+
}
163+
164+
.month {
165+
width: 200px;
166+
}
167+
168+
/* 年份 */
169+
.year {
170+
width: 35px;
171+
height: 20px;
172+
position: absolute;
173+
top: 0px;
174+
left: 0px;
175+
right: 0px;
176+
bottom: 0px;
177+
margin: auto;
178+
color: #fff;
179+
}
180+
181+
span {
182+
display: inline-block;
183+
width: 60px;
184+
height: 20px;
185+
text-align: center;
186+
}
187+
188+
/* 旋转 */
189+
.tra {
190+
transform: rotate(180deg);
191+
}
192+
193+
/* 白色透明标注盒子 */
194+
.show {
195+
width: 800px;
196+
height: 20px;
197+
position: absolute;
198+
top: 0px;
199+
left: 0px;
200+
right: 0px;
201+
bottom: 0px;
202+
margin: auto;
203+
}
204+
205+
.show .right {
206+
float: right;
207+
width: 420px;
208+
height: 20px;
209+
background-color: rgba(255, 255, 255, 0.4);
210+
}

纯js时间罗盘/index.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>纯 JS 时间罗盘</title>
7+
<link rel="stylesheet" href="./index.css">
8+
</head>
9+
10+
<body>
11+
12+
<div id="box">
13+
<!-- 白色透明标注盒子 -->
14+
<div class="show">
15+
<div class="right"></div>
16+
</div>
17+
</div>
18+
19+
<script src="./index.js"></script>
20+
</body>
21+
22+
</html>

纯js时间罗盘/index.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* 创建出每个装时间的盒子
3+
* 包括:
4+
* 秒/分钟/小时/日/月/年
5+
*/
6+
const box = document.getElementById('box')
7+
8+
/**
9+
* 创建出每个装时间的盒子
10+
* @param {String} labelName 创建的标签的 class 名称
11+
*/
12+
function addBigBox(labelName) {
13+
const newLabel = document.createElement('div')
14+
newLabel.setAttribute('class', labelName)
15+
box.appendChild(newLabel)
16+
}
17+
18+
addBigBox('secondBox')
19+
addBigBox('branchBox')
20+
addBigBox('hourBox')
21+
addBigBox('NumberBox')
22+
addBigBox('monthBox')
23+
addBigBox('year')
24+
25+
/**
26+
* 获取到所有装时间的盒子
27+
*/
28+
const secondBox = document.querySelector('.secondBox')
29+
const branchBox = document.querySelector('.branchBox')
30+
const hourBox = document.querySelector('.hourBox')
31+
const NumberBox = document.querySelector('.NumberBox')
32+
const monthBox = document.querySelector('.monthBox')
33+
const year = document.querySelector('.year')
34+
// 默认设置 2021 年
35+
year.innerHTML = '2021'
36+
37+
38+
/**
39+
* 渲染时间的标签内容
40+
* @param {String} PointerName 指针的名称
41+
* @param {Object} bigTabName 需要放到的盒子 DOM 元素
42+
* @param {String} explain 说明文字
43+
* @param {Number} number 循环次数
44+
* @param {Number} deg 旋转度数
45+
*/
46+
function Rendering(PointerName, bigTabName, explain, number, deg) {
47+
for (let i = 1; i < number; i++) {
48+
// 新建一个 div 标签 设置 class 名称和旋转角度
49+
const Pointer = document.createElement('div')
50+
Pointer.setAttribute('class', PointerName)
51+
Pointer.style.transform = `rotate(${i * deg}deg)`
52+
53+
54+
// 把新建的 div 都放在大盒子中
55+
bigTabName.appendChild(Pointer)
56+
// 里面分别新建两个 span 标签用来显示时间内容描述
57+
const newSpan1 = document.createElement('span')
58+
const newSpan2 = document.createElement('span')
59+
newSpan1.innerHTML = i + (number - 1) + explain
60+
newSpan2.innerHTML = i + explain
61+
62+
// 给新建的 span 标签设置 class 值 并将 span 标签放到新建的 div 中
63+
newSpan1.setAttribute('class', 'tra')
64+
Pointer.appendChild(newSpan1)
65+
Pointer.appendChild(newSpan2)
66+
}
67+
}
68+
69+
Rendering('second', secondBox, '秒', 31, 6)
70+
Rendering('branch', branchBox, '分', 31, 6)
71+
Rendering('hour', hourBox, '时', 7, 30)
72+
Rendering('Number', NumberBox, '号', 17, 11.25)
73+
Rendering('month', monthBox, '月', 7, 30)
74+
75+
76+
/**
77+
* 清空指针的 60 分钟
78+
* 清空指针的 60 秒
79+
* 清空指针的 32 号
80+
* @param {String} className Class 值
81+
*/
82+
function subItem(className) {
83+
const item = document.querySelectorAll(className)
84+
item[item.length - 1].children[0].innerHTML = ''
85+
}
86+
87+
subItem('.second')
88+
subItem('.branch')
89+
subItem('.Number')
90+
91+
92+
/**
93+
* 等待1.4秒后css动画结束后再获取时间
94+
*/
95+
setTimeout(() => {
96+
setInterval(() => {
97+
const now = new Date()
98+
const s = now.getSeconds() // 秒
99+
const m = now.getMinutes() // 分
100+
const h = now.getHours() % 12 // 小时
101+
const d = now.getDate() // 日
102+
const y = now.getMonth() + 1 // 月
103+
const n = now.getFullYear() // 年
104+
105+
secondBox.style.transform = `rotate(-${s * 6}deg)` // 秒
106+
branchBox.style.transform = `rotate(-${m * 6}deg)` // 分
107+
hourBox.style.transform = `rotate(-${h * 30}deg)` // 时
108+
NumberBox.style.transform = `rotate(-${d * 11.25}deg)` // 日
109+
monthBox.style.transform = `rotate(-${y * 30}deg)` // 月
110+
year.innerHTML = n // 年
111+
112+
}, 1000)
113+
}, 1400)

0 commit comments

Comments
 (0)