|
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 min-hand"></div> |
| 12 | + <div class="hand second-hand"></div> |
16 | 13 | </div>
|
17 |
| - |
| 14 | + </div> |
18 | 15 |
|
19 | 16 | <style>
|
20 | 17 | html {
|
|
56 | 53 | }
|
57 | 54 |
|
58 | 55 | .hand {
|
| 56 | + position: absolute; |
59 | 57 | width:50%;
|
60 | 58 | height:6px;
|
61 |
| - background:black; |
62 |
| - position: absolute; |
63 | 59 | top:50%;
|
| 60 | + background:black; |
64 | 61 | transform-origin: 100%;
|
65 |
| - transform: rotate(90deg); |
66 | 62 | transition: all 0.05s;
|
67 | 63 | transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1);
|
68 | 64 | }
|
69 |
| -</style> |
70 | 65 |
|
71 |
| -<script> |
72 |
| - const secondHand = document.querySelector('.second-hand'); |
73 |
| - const minsHand = document.querySelector('.min-hand'); |
74 |
| - const hourHand = document.querySelector('.hour-hand'); |
| 66 | + .hand.hour-hand { |
| 67 | + width:30%; |
| 68 | + left: 20%; |
| 69 | + } |
| 70 | + .hand.min-hand { |
| 71 | + width:40%; |
| 72 | + left: 10%; |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + </style> |
75 | 77 |
|
76 |
| - function setDate() { |
77 |
| - const now = new Date(); |
| 78 | + <script> |
| 79 | + class Clock { |
| 80 | + constructor() { |
| 81 | + let klass = this; |
78 | 82 |
|
79 |
| - const seconds = now.getSeconds(); |
80 |
| - const secondsDegrees = ((seconds / 60) * 360) + 90; |
81 |
| - secondHand.style.transform = `rotate(${secondsDegrees}deg)`; |
| 83 | + this.secondHand = document.querySelector('.hand.second-hand'); |
| 84 | + this.minuteHand = document.querySelector('.hand.min-hand'); |
| 85 | + this.hourHand = document.querySelector('.hand.hour-hand'); |
82 | 86 |
|
83 |
| - const mins = now.getMinutes(); |
84 |
| - const minsDegrees = ((mins / 60) * 360) + 90; |
85 |
| - minsHand.style.transform = `rotate(${minsDegrees}deg)`; |
| 87 | + this.redraw(); |
| 88 | + window.setInterval(function() { klass.redraw() }, 1000); |
| 89 | + } |
86 | 90 |
|
87 |
| - const hour = now.getMinutes(); |
88 |
| - const hourDegrees = ((mins / 12) * 360) + 90; |
89 |
| - hourHand.style.transform = `rotate(${hourDegrees}deg)`; |
90 |
| - } |
| 91 | + redraw() { |
| 92 | + let time = new Date(); |
91 | 93 |
|
92 |
| - setInterval(setDate, 1000); |
| 94 | + this.setHandTo(this.secondHand, 360 / 60 * time.getSeconds()); |
| 95 | + this.setHandTo(this.minuteHand, 360 / 60 * time.getMinutes()); |
| 96 | + this.setHandTo(this.hourHand, 360 / 12 * time.getHours()); |
| 97 | + } |
| 98 | + |
| 99 | + setHandTo(handElement, degree) { |
| 100 | + // initial state is on 45min (270deg), so we have to adopt the rotation to this |
| 101 | + let fixedDegree = 90 + degree; |
| 102 | + if (fixedDegree >= 360) { |
| 103 | + fixedDegree = fixedDegree - 360; |
| 104 | + } |
| 105 | + |
| 106 | + handElement.setAttribute('style', `transform: rotate(${fixedDegree}deg)`); |
| 107 | + } |
| 108 | + } |
93 | 109 |
|
94 |
| -</script> |
| 110 | + new Clock(); |
| 111 | + </script> |
95 | 112 | </body>
|
96 | 113 | </html>
|
0 commit comments