|
61 | 61 | background:black;
|
62 | 62 | position: absolute;
|
63 | 63 | top:50%;
|
| 64 | + transform-origin: 100%; |
| 65 | + transform: rotate(90deg); |
| 66 | + transition: all 0.05s; /* I like .5s if the hand is not moving as far each time */ |
| 67 | + transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1); /* There is a curve picker in Chrome dev tools. Sweet! */ |
64 | 68 | }
|
65 | 69 |
|
66 | 70 | </style>
|
67 | 71 |
|
68 | 72 | <script>
|
| 73 | + const secondHand = document.querySelector(".second-hand"); |
| 74 | + const minuteHand = document.querySelector(".min-hand"); |
| 75 | + const hourHand = document.querySelector(".hour-hand"); |
69 | 76 |
|
| 77 | + function setDate(){ |
| 78 | + const currentTime = new Date(); |
| 79 | + |
| 80 | + const seconds = currentTime.getSeconds(); |
| 81 | + const secondsDegrees = (((seconds/60) * 360) + 90); // convert from minute time (60s) |
| 82 | + // to base ten (over 100), then multiple by degrees in a circle |
| 83 | + // and add 90 to account for the rotation we gave it by default in the CSS |
| 84 | + secondHand.style.transform = `rotate(${secondsDegrees}deg)`; |
| 85 | + |
| 86 | + const minutes = currentTime.getMinutes(); |
| 87 | + const minutesDegrees = (((minutes/60) * 360) + 90); // hand moves same distance as |
| 88 | + // for seconds, seconds and minutes share same stops on a clock |
| 89 | + minuteHand.style.transform = `rotate(${minutesDegrees}deg)`; |
70 | 90 |
|
| 91 | + const hours = currentTime.getHours(); |
| 92 | + const hoursDegrees = (((hours/12) * 360) + 90); // 12 stops on a clock for hours |
| 93 | + hourHand.style.transform = `rotate(${hoursDegrees}deg)`; |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + setInterval(setDate, 1000); |
| 98 | + |
| 99 | + |
| 100 | + /* |
| 101 | + new things I learned: |
| 102 | + - The CSS transform property. With it, elements can be translated, rotated, scaled, and skewed. |
| 103 | + - There is a cubic bezier editor in Chrome dev tools that lets you pick just the right curve. Add the transition-timing-function property to an element and then click on the small purple icon next to the value. |
| 104 | + - Practiced using some of the prototype methods for Date(), such as getSeconds(). |
| 105 | + - Had to think about how a clock works and do a bit of math. |
| 106 | + */ |
71 | 107 | </script>
|
72 | 108 | </body>
|
73 | 109 | </html>
|
0 commit comments