|
61 | 61 | background:black; |
62 | 62 | position: absolute; |
63 | 63 | top:50%; |
| 64 | + transform-origin: 100%; /* along x-axis, 0% would be left, 100% is at right. |
| 65 | + so this will make the origin point the right side, |
| 66 | + which in our case is the pivot point in the center |
| 67 | + of the clock face. otherwise each hand spins |
| 68 | + at the center of the hand, not at one end. */ |
| 69 | + transform: rotate(90deg); /* rotate all the hands 90º so they start at 12 */ |
| 70 | + transition: all 0.05s; /* how long the transition takes (the 1-sec "tick" )*/ |
| 71 | + transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1); |
| 72 | + /* this defines how the hand moves during transition. this one moves and |
| 73 | + bounces back a little, for a fun effect. there's like a little |
| 74 | + gui editor in chrome dev tools you can use for this?? */ |
64 | 75 | } |
65 | 76 |
|
66 | 77 | </style> |
67 | 78 |
|
68 | 79 | <script> |
| 80 | + const secondHand = document.querySelector('.second-hand'); |
| 81 | + const minuteHand = document.querySelector('.min-hand'); |
| 82 | + const hourHand = document.querySelector('.hour-hand'); |
69 | 83 |
|
| 84 | + function setDate() { |
| 85 | + const now = new Date(); |
| 86 | + |
| 87 | + const seconds = now.getSeconds(); |
| 88 | + // calculate the seconds |
| 89 | + // first, divide seconds by 60, this gives you a percentage |
| 90 | + // e.g. 5 / 60 = 8.3% |
| 91 | + // multiply that percentage by 360 degrees to get current position |
| 92 | + // add 90 to that to offset the fact that we started at 90, not 0. |
| 93 | + const secondsDegrees = ((seconds / 60) * 360) + 90; |
| 94 | + // set the transform property so we rotate that number of degrees |
| 95 | + secondHand.style.transform = `rotate(${secondsDegrees}deg)`; |
| 96 | + |
| 97 | + const minutes = now.getMinutes(); |
| 98 | + const minutesDegrees = ((minutes / 60) * 360) + 90; |
| 99 | + minuteHand.style.transform = `rotate(${minutesDegrees}deg)`; |
| 100 | + |
| 101 | + const hours = now.getHours(); |
| 102 | + // divide by 12, not 60 |
| 103 | + const hoursDegrees = ((hours / 12) * 360) + 90; |
| 104 | + hourHand.style.transform = `rotate(${hoursDegrees}deg)`; |
| 105 | + } |
| 106 | + |
| 107 | + // run this once per 1000ms (1s) |
| 108 | + setInterval(setDate, 1000); |
70 | 109 |
|
71 | 110 | </script> |
72 | 111 | </body> |
|
0 commit comments