|
1 | 1 | <!DOCTYPE html>
|
2 | 2 | <html lang="en">
|
3 | 3 | <head>
|
4 |
| - <meta charset="UTF-8"> |
5 |
| - <title>JS + CSS Clock</title> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>JS + CSS Clock</title> |
| 6 | + |
| 7 | + <style> |
| 8 | + html { |
| 9 | + background: #018DED url(http://unsplash.it/1500/1000?image=881&blur=50); |
| 10 | + background-size: cover; |
| 11 | + font-family: 'helvetica neue', serif; |
| 12 | + text-align: center; |
| 13 | + font-size: 10px; |
| 14 | + } |
| 15 | + |
| 16 | + body { |
| 17 | + margin: 0; |
| 18 | + font-size: 2rem; |
| 19 | + display: flex; |
| 20 | + flex: 1; |
| 21 | + min-height: 100vh; |
| 22 | + align-items: center; |
| 23 | + } |
| 24 | + |
| 25 | + .clock { |
| 26 | + width: 30rem; |
| 27 | + height: 30rem; |
| 28 | + border: 20px solid white; |
| 29 | + border-radius: 50%; |
| 30 | + margin: 50px auto; |
| 31 | + position: relative; |
| 32 | + padding: 2rem; |
| 33 | + box-shadow: 0 0 0 4px rgba(0, 0, 0, 0.1), |
| 34 | + inset 0 0 0 3px #EFEFEF, |
| 35 | + inset 0 0 10px black, |
| 36 | + 0 0 10px rgba(0, 0, 0, 0.2); |
| 37 | + } |
| 38 | + |
| 39 | + .clock-face { |
| 40 | + position: relative; |
| 41 | + width: 100%; |
| 42 | + height: 100%; |
| 43 | + transform: translateY(-3px); /* account for the height of the clock hands */ |
| 44 | + } |
| 45 | + |
| 46 | + .hand { |
| 47 | + width: 50%; |
| 48 | + height: 6px; |
| 49 | + background: black; |
| 50 | + position: absolute; |
| 51 | + top: 50%; |
| 52 | + transform-origin: 100%; |
| 53 | + transform: rotate(90deg); |
| 54 | + transition: all 0.05s; |
| 55 | + transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1); |
| 56 | + } |
| 57 | + </style> |
6 | 58 | </head>
|
7 | 59 | <body>
|
8 | 60 |
|
9 |
| - |
10 |
| - <div class="clock"> |
11 |
| - <div class="clock-face"> |
| 61 | +<div class="clock"> |
| 62 | + <div class="clock-face"> |
12 | 63 | <div class="hand hour-hand"></div>
|
13 | 64 | <div class="hand min-hand"></div>
|
14 | 65 | <div class="hand second-hand"></div>
|
15 |
| - </div> |
16 | 66 | </div>
|
| 67 | +</div> |
17 | 68 |
|
| 69 | +<script> |
| 70 | + const secondHand = document.querySelector('.second-hand'); |
| 71 | + const minsHand = document.querySelector('.min-hand'); |
| 72 | + const hourHand = document.querySelector('.hour-hand'); |
18 | 73 |
|
19 |
| - <style> |
20 |
| - html { |
21 |
| - background:#018DED url(http://unsplash.it/1500/1000?image=881&blur=50); |
22 |
| - background-size:cover; |
23 |
| - font-family:'helvetica neue'; |
24 |
| - text-align: center; |
25 |
| - font-size: 10px; |
26 |
| - } |
| 74 | + function setDate() { |
| 75 | + const now = new Date(); |
| 76 | + const seconds = now.getSeconds(); |
| 77 | + const secondsDegrees = ((seconds / 60) * 360) + 90; |
27 | 78 |
|
28 |
| - body { |
29 |
| - margin: 0; |
30 |
| - font-size: 2rem; |
31 |
| - display:flex; |
32 |
| - flex:1; |
33 |
| - min-height: 100vh; |
34 |
| - align-items: center; |
35 |
| - } |
36 |
| - |
37 |
| - .clock { |
38 |
| - width: 30rem; |
39 |
| - height: 30rem; |
40 |
| - border:20px solid white; |
41 |
| - border-radius:50%; |
42 |
| - margin:50px auto; |
43 |
| - position: relative; |
44 |
| - padding:2rem; |
45 |
| - box-shadow: |
46 |
| - 0 0 0 4px rgba(0,0,0,0.1), |
47 |
| - inset 0 0 0 3px #EFEFEF, |
48 |
| - inset 0 0 10px black, |
49 |
| - 0 0 10px rgba(0,0,0,0.2); |
50 |
| - } |
| 79 | + secondHand.style.transform = `rotate(${secondsDegrees}deg)`; |
51 | 80 |
|
52 |
| - .clock-face { |
53 |
| - position: relative; |
54 |
| - width: 100%; |
55 |
| - height: 100%; |
56 |
| - transform: translateY(-3px); /* account for the height of the clock hands */ |
57 |
| - } |
| 81 | + const mins = now.getMinutes(); |
| 82 | + const minsDegrees = ((mins / 60) * 360) + 90; |
| 83 | + minsHand.style.transform = `rotate(${minsDegrees}deg`; |
58 | 84 |
|
59 |
| - .hand { |
60 |
| - width:50%; |
61 |
| - height:6px; |
62 |
| - background:black; |
63 |
| - position: absolute; |
64 |
| - top:50%; |
| 85 | + const hours = now.getHours(); |
| 86 | + const hoursDegrees = ((hours / 12) * 360) + 90; |
| 87 | + hourHand.style.transform = `rotate(${hoursDegrees}deg`; |
65 | 88 | }
|
66 | 89 |
|
67 |
| - </style> |
68 |
| - |
69 |
| - <script> |
70 |
| - |
| 90 | + setInterval(setDate, 1000); |
| 91 | +</script> |
71 | 92 |
|
72 |
| - </script> |
73 | 93 | </body>
|
74 | 94 | </html>
|
0 commit comments