Skip to content

Commit 8cd9f4e

Browse files
committed
Add js script for animating clock hands
1 parent db95a87 commit 8cd9f4e

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

02 - JS + CSS Clock/index-START.html

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
<div class="clock-face">
1212
<div class="hand hour-hand"></div>
1313
<div class="hand min-hand"></div>
14-
<div class="hand second-hand" style="
15-
transform: rotate(-50deg);"></div>
14+
<div class="hand second-hand"></div>
1615
</div>
1716
</div>
1817

@@ -76,6 +75,27 @@
7675
</style>
7776

7877
<script>
78+
const secondHand = document.querySelector('.second-hand');
79+
const hourHand = document.querySelector('.hour-hand');
80+
const minuteHand = document.querySelector('.min-hand');
81+
82+
function setDate() {
83+
const now = new Date();
84+
const seconds = now.getSeconds();
85+
const secondsDegrees = ((seconds / 60) * 360) + 90;
86+
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
87+
88+
const mins = now.getMinutes();
89+
const minsDegrees = ((mins / 60) * 360) + 90;
90+
minuteHand.style.transform = `rotate(${minsDegrees}deg)`
91+
92+
const hour = now.getHours();
93+
const hourDegrees = ((hour / 12) * 360) + 90;
94+
hourHand.style.transform = `rotate(${hourDegrees}deg)`
95+
console.log(seconds);
96+
}
97+
98+
setInterval(setDate, 1000);
7999
</script>
80100
</body>
81101
</html>

0 commit comments

Comments
 (0)