forked from wdaweb/113-2-js_clock-wdaweb-clock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.html
421 lines (370 loc) · 10.7 KB
/
clock.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Fugaz+One&family=Montserrat:ital,wght@0,100..900;1,100..900&family=Noto+Sans+TC:wght@100..900&display=swap"
rel="stylesheet">
<style>
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
.fugaz-one-regular {
font-family: "Fugaz One", sans-serif;
font-weight: 400;
font-style: normal;
}
#container {
display: flex;
justify-content: center;
align-items: center;
height: 90vh;
width: 100vw;
overflow: hidden;
margin: auto;
}
#text {
text-align: left;
width: 85%;
position: absolute;
top: 50%;
left: 50%;
z-index: 0;
font-size: calc(19vw);
white-space: nowrap;
color: rgba(255, 255, 255, 0.3);
margin: 0;
visibility: hidden;
animation: float 6s ease-in-out infinite;
}
#startButton {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 1.5rem;
padding: 10px 20px;
cursor: pointer;
transition: all 0.5s ease;
}
#startButton.top {
position: fixed;
top: 0;
left: 50%;
transform: translateX(-50%);
font-size: 1.2rem;
padding: 8px 16px;
background: #000;
color: #fff;
border: none;
border-radius: 5px;
display: block;
animation: fadeIn 0.5s ease;
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
:root {
--accent: hsl(360, 80%, 50%);
--accent-darken-12: hsl(360, 80%, 38%);
--accent-darken-20: hsl(360, 80%, 30%);
--accent-lighten-20: hsl(360, 80%, 70%);
}
.rnOuter {
background: var(--accent-darken-12);
overflow: hidden;
position: relative;
height: 100vh;
}
.rnInner {
width: 100%;
position: absolute;
top: -10%;
right: 0;
bottom: 0;
left: 0;
margin: auto;
transform-style: preserve-3d;
transition: transform 6s ease;
transform-origin: -120% top;
}
.rnUnit {
width: 15vw;
height: 120vh;
background: repeating-linear-gradient(to left,
var(--accent) 4vw,
var(--accent-darken-20) 8vw,
var(--accent-lighten-20) 10vw);
background-size: 100% 100%;
display: inline-block;
transform-origin: 0 0%;
transform: rotate(3deg);
animation: rnUnit 2s ease infinite;
}
@keyframes rnUnit {
50% {
transform: rotate(-3deg);
}
}
/* 為每個 rnUnit 添加不同的動畫延遲 */
.rnUnit:nth-child(1) {
animation-delay: -0.1s;
}
.rnUnit:nth-child(2) {
animation-delay: -0.2s;
}
.rnUnit:nth-child(3) {
animation-delay: -0.3s;
}
.rnUnit:nth-child(4) {
animation-delay: -0.4s;
}
.rnUnit:nth-child(5) {
animation-delay: -0.5s;
}
.rnUnit:nth-child(6) {
animation-delay: -0.6s;
}
.rnUnit:nth-child(7) {
animation-delay: -0.7s;
}
.rnUnit:nth-child(8) {
animation-delay: -0.8s;
}
.rnUnit:nth-child(9) {
animation-delay: -0.9s;
}
.rnUnit:nth-child(10) {
animation-delay: -1s;
}
.aoTable {
width: 100%;
height: 100vh;
text-align: center;
position: relative;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.rnOuter:hover .rnInner {
transform-origin: -120% top;
transform: scaleX(3);
}
#clock {
width: 300px;
height: 300px;
background: url(./img/clockFace.png) no-repeat center center fixed;
opacity: 0.5;
box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.3);
border-radius: 50%;
position: relative;
transition: transform 0.1s ease;
transform-style: preserve-3d;
will-change: transform;
visibility: hidden;
/* 初始隱藏,但在 DOM 中存在 */
opacity: 0;
perspective: 800px;
}
@keyframes clockAnimation {
0% {
transform: translateY(100vh) rotateY(0deg) scale(90%);
/* 初始狀態:沿Y軸旋轉 0 度 */
opacity: 0;
}
60% {
transform: translateY(-200px) rotateY(1080deg) rotateX(30deg) scale(120%);
/* 沿Y軸旋轉 3 圈 */
opacity: 1;
}
80% {
transform: translateY(10px) rotateY(1080deg) rotateX(-30deg) scale(110%);
opacity: 1;
}
87% {
transform: translateY(-5px) rotateY(1080deg) rotateX(10deg) scale(100%);
/* 減小彈跳高度 */
opacity: 1;
}
95% {
transform: translateY(5px) rotateY(1080deg) rotateX(-10deg) scale(98%);
opacity: 1;
}
100% {
transform: translateY(0) rotateY(1080deg) rotateX(0deg);
/* 回到原位 */
opacity: 1;
}
}
@keyframes float {
0% {
dropshadow: 0 5px 15px 0px rgba(0, 0, 0, 0.6);
transform: translate(-50%, -50%);
}
50% {
dropshadow: 0 25px 15px 0px rgba(0, 0, 0, 0.2);
transform: translate(-50%, -55%);
}
100% {
dropshadow: 0 5px 15px 0px rgba(0, 0, 0, 0.6);
transform: translate(-50%, -50%);
}
}
.show {
visibility: visible;
opacity: 1;
animation: clockAnimation 3s cubic-bezier(0.42, 0, 0.58, 1);
}
/* Slight parallax effect on hover */
#clock:hover .content {
transform: rotate3D(1, 1, 0, 2deg) translateZ(15px);
}
.content {
width: 300px;
height: 300px;
position: relative;
z-index: 1;
transition: transform 0.3s ease;
}
#hour {
width: 20%;
/* 粗細 */
height: 28px;
/* background-color: blue; */
background-image: url(./img/h_hand.png);
border-radius: 5px;
position: absolute;
left: 30%;
/* 減去他粗細的一半,讓中心在圓心 */
top: calc(50% - 5px);
transform-origin: right center;
transform: rotate(0deg);
}
#minute {
width: 30%;
height: 18px;
background-image: url(./img/m_hand.png);
position: absolute;
left: 20%;
top: calc(50% - 4px);
transform-origin: right center;
transform: rotate(0deg);
}
#second {
width: 45%;
height: 12px;
background-image: url(./img/s_hand.png);
border-radius: 5px;
position: absolute;
left: 5%;
/* 減去他粗細的一半,讓中心在圓心 */
top: calc(50% - 2px);
transform-origin: right center;
transform: rotate(0deg);
}
</style>
</head>
<body>
<div class="rxWorld">
<section class="rnOuter ">
<div class="aoTable">
<button id="startButton">現在幾點?</button>
<div id="text" class="fugaz-one-regular">00:00:00</div>
<div id="container">
<div id="clock">
<div class="content">
<div id="hour"></div>
<div id="minute"></div>
<div id="second"></div>
</div>
</div>
</div>
<div>
<div class='rnInner'>
<div class='rnUnit'></div>
<div class='rnUnit'></div>
<div class='rnUnit'></div>
<div class='rnUnit'></div>
<div class='rnUnit'></div>
<div class='rnUnit'></div>
<div class='rnUnit'></div>
<div class='rnUnit'></div>
<div class='rnUnit'></div>
<div class='rnUnit'></div>
<div class='rnUnit'></div>
</div>
</section>
</div>
<script>
const button = document.getElementById('startButton');
const elClock = document.getElementById('clock');
const elText = document.getElementById('text')
button.addEventListener("click", function () {
button.style.display = "none";
elClock.style.visibility = 'visible';
elText.style.visibility = 'hidden';
elClock.style.opacity = '1';
// 加動畫 class
elClock.classList.add('show');
setTimeout(() => {
button.style.display = "block";
button.classList.add("top");
}, 500);
// 當動畫結束後,移除動畫 class,顯示文字
elClock.addEventListener('animationend', () => {
elClock.classList.remove('show');
elText.style.visibility = 'visible';
});
});
// threshold 變數 用來控制旋轉的幅度或範圍
const THRESHOLD = 30;
const elHour = document.getElementById('hour')
const elMinute = document.getElementById('minute')
const elSecond = document.getElementById('second')
const clock = () => {
const date = new Date()
const h = date.getHours()
const m = date.getMinutes()
const s = date.getSeconds()
elHour.style.transform = `rotateZ(${h * 30 + (m / 59 * 30) + 90}deg) rotateX(0deg)`
elMinute.style.transform = `rotateZ(${m * 6 + 90}deg) rotateX(0deg)`
elSecond.style.transform = `rotateZ(${s * 6 + 90}deg) rotateX(0deg)`
elText.innerText = `${h.toString().padStart(2, '0')}:${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`
// padStart 是要幫時鐘數字補上零啦
}
// 搖來搖去假視差
elClock.addEventListener("mousemove", handleHover);
elClock.addEventListener("mouseleave", resetStyles);
function handleHover(e) {
const { clientX, clientY, currentTarget } = e;
const { clientWidth, clientHeight, offsetLeft, offsetTop } = currentTarget;
// 計算滑鼠在水平和垂直方向的相對位置(範圍 0 到 1)
const horizontal = (clientX - offsetLeft) / clientWidth;
const vertical = (clientY - offsetTop) / clientHeight;
// 將滑鼠的相對位置線性映射到 -THRESHOLD 到 THRESHOLD 的範圍
// const rotateX = (THRESHOLD / 2 - horizontal * THRESHOLD).toFixed(2);
// const rotateY = (horizontal * THRESHOLD - THRESHOLD / 2).toFixed(2);
const rotateX = (THRESHOLD * (0.5 - vertical)).toFixed(2); // 垂直方向: 中心點為 0
const rotateY = (THRESHOLD * (horizontal - 0.5)).toFixed(2); // 水平方向: 中心點為 0
elClock.style.transform = `perspective(800px) rotateX(${rotateX}deg) rotateY(${rotateY}deg)`;
}
function resetStyles(e) {
elClock.style.transform =
`perspective(${e.currentTarget.clientWidth}px) rotateX(0deg) rotateY(0deg)`;
}
setInterval(clock, 1000)
</script>
</body>
</html>