|
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 hour-hand no-transition"></div> |
| 13 | + <div class="hand min-hand no-transition"></div> |
| 14 | + <div class="hand second-hand" style=""></div> |
15 | 15 | </div> |
16 | 16 | </div> |
17 | 17 |
|
|
62 | 62 | background:black; |
63 | 63 | position: absolute; |
64 | 64 | top:50%; |
| 65 | + transform-origin: 100%; |
| 66 | + transform:rotate(90deg); |
| 67 | + transition: all 0.5s; |
| 68 | + transition-timing-function: cubic-bezier(0.04, 1.77, 0.32, 0.94); |
| 69 | + } |
| 70 | + hand.no-transition { |
| 71 | + transition:none; |
65 | 72 | } |
66 | | - |
67 | 73 | </style> |
68 | 74 |
|
69 | 75 | <script> |
| 76 | + const hourhand = document.querySelector('.hour-hand') |
| 77 | + const minutehand = document.querySelector('.min-hand') |
| 78 | + const secondhand = document.querySelector('.second-hand') |
| 79 | + |
| 80 | + function setHands(h,m,s){ |
| 81 | + secondhand.style.transform = `rotate(${s/60 * 360 + 90}deg)`; |
| 82 | + minutehand.style.transform = `rotate(${m/60 * 360 + 90}deg)`; |
| 83 | + hourhand.style.transform = `rotate(${h/12 * 360 + 90}deg)`; |
| 84 | + |
| 85 | + console.log(s, `rotate(${s/60 * 360 + 90}deg)`) |
| 86 | + } |
| 87 | + |
| 88 | + function setTime() { |
| 89 | + const now = new Date(); |
| 90 | + let seconds = now.getSeconds(); |
| 91 | + const minutes = now.getMinutes(); |
| 92 | + const hours = now.getHours(); |
| 93 | + |
| 94 | + //fix the weird slide transition from 359degrees -> 0degrees |
| 95 | + if(seconds === 0){ |
| 96 | + seconds = 60; |
| 97 | + } |
| 98 | + |
| 99 | + setHands(hours,minutes,seconds) |
| 100 | + |
| 101 | + //tried, doesn't work.. still animates |
| 102 | + if(seconds === 60){ |
| 103 | + console.log('add class') |
| 104 | + secondhand.style.transition = 'all 0s'; |
| 105 | + setHands(hours,minutes,0) |
| 106 | + console.log('remove class') |
| 107 | + secondhand.style.transition = 'all 0.5s'; |
| 108 | + // window.setTimeout( () => { |
| 109 | + // // secondhand.classList.remove('no-transition') |
| 110 | + // }, 0) |
| 111 | + } |
| 112 | + } |
70 | 113 |
|
| 114 | + setTime(); |
| 115 | + window.setInterval(setTime, 1000); |
71 | 116 |
|
72 | 117 | </script> |
73 | 118 | </body> |
|
0 commit comments