|
9 | 9 |
|
10 | 10 | <div class="clock"> |
11 | 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> |
| 12 | + <div class="hand js-hour-hand"></div> |
| 13 | + <div class="hand js-minute-hand"></div> |
| 14 | + <div class="hand js-second-hand"></div> |
15 | 15 | </div> |
16 | 16 | </div> |
17 | 17 |
|
|
58 | 58 | .hand { |
59 | 59 | width:50%; |
60 | 60 | height:6px; |
61 | | - background:black; |
| 61 | + background-color: black; |
62 | 62 | position: absolute; |
63 | 63 | top:50%; |
64 | | - transform-origin: 100%; |
65 | | - transform: rotate(90deg); |
| 64 | + transform-origin: 100%; /* make the right end the pivot point of rotation, not the middle (default = 50%) */ |
| 65 | + transform: rotate(90deg); /* flip the hand from horizontal, to vertical pointing at 12:00; */ |
66 | 66 | transition: all 0.05s; |
67 | | - transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1); |
| 67 | + transition-timing-function: ease-in-out; /* helps mimic second hand tick action */ |
| 68 | + z-index: 25; |
68 | 69 | } |
69 | | -</style> |
70 | 70 |
|
71 | | -<script> |
72 | | - const secondHand = document.querySelector('.second-hand'); |
73 | | - const minsHand = document.querySelector('.min-hand'); |
74 | | - const hourHand = document.querySelector('.hour-hand'); |
75 | | - |
76 | | - function setDate() { |
77 | | - const now = new Date(); |
78 | | - |
79 | | - const seconds = now.getSeconds(); |
80 | | - const secondsDegrees = ((seconds / 60) * 360) + 90; |
81 | | - secondHand.style.transform = `rotate(${secondsDegrees}deg)`; |
82 | | - |
83 | | - const mins = now.getMinutes(); |
84 | | - const minsDegrees = ((mins / 60) * 360) + 90; |
85 | | - minsHand.style.transform = `rotate(${minsDegrees}deg)`; |
86 | | - |
87 | | - const hour = now.getHours(); |
88 | | - const hourDegrees = ((hour / 12) * 360) + 90; |
89 | | - hourHand.style.transform = `rotate(${hourDegrees}deg)`; |
90 | | - } |
| 71 | + .js-second-hand { |
| 72 | + background-color: red; |
| 73 | + height: 2px; |
| 74 | + z-index: 10; |
| 75 | + } |
91 | 76 |
|
92 | | - setInterval(setDate, 1000); |
| 77 | + </style> |
| 78 | + |
| 79 | + <script> |
| 80 | + const secondHand = document.querySelector('.js-second-hand'); |
| 81 | + const minuteHand = document.querySelector('.js-minute-hand'); |
| 82 | + const hourHand = document.querySelector('.js-hour-hand'); |
| 83 | + |
| 84 | + function setDate() { |
| 85 | + const now = new Date(); |
| 86 | + const seconds = now.getSeconds(); |
| 87 | + const secondsDegrees = ((seconds / 60) * 360) + 90; /* for every second move the second hand 6 degrees */ |
| 88 | + secondHand.style.transform = `rotate(${secondsDegrees}deg)`; /* */ |
| 89 | + // console.log('seconds= ' + seconds); |
| 90 | + |
| 91 | + const minutes = now.getMinutes(); |
| 92 | + const minutesDegrees = ((minutes / 60) * 360) + 90; |
| 93 | + minuteHand.style.transform = `rotate(${minutesDegrees}deg)`; |
| 94 | + // console.log('minutes= ' + minutes); |
| 95 | + |
| 96 | + const hours = now.getHours(); |
| 97 | + const hoursDegrees = ((hours / 12) * 360) + 90; |
| 98 | + hourHand.style.transform = `rotate(${hoursDegrees}deg)`; |
| 99 | + // console.log('hours= ' + hours); |
| 100 | + } |
93 | 101 |
|
94 | | - setDate(); |
| 102 | + setInterval(setDate, 1000); /* run function every 1 second */ |
95 | 103 |
|
96 | | -</script> |
| 104 | + </script> |
97 | 105 | </body> |
98 | 106 | </html> |
0 commit comments