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