Visitar URL original
Build Clock function · wesbos/JavaScript30@45b5cc1 · GitHub
Skip to content

Commit 45b5cc1

Browse files
committed
Build Clock function
1 parent 784c97c commit 45b5cc1

1 file changed

Lines changed: 38 additions & 1 deletion

File tree

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<div class="clock">
1111
<div class="clock-face">
1212
<div class="hand hour-hand"></div>
13-
<div class="hand min-hand"></div>
13+
<div class="hand minute-hand"></div>
1414
<div class="hand second-hand"></div>
1515
</div>
1616
</div>
@@ -61,12 +61,49 @@
6161
background:black;
6262
position: absolute;
6363
top:50%;
64+
transform-origin: 100%;
65+
transform: rotate(90deg);
66+
transition: all 0.05s;
67+
transition-timing-function: cubic-bezier(0.1, 2.7, 0.5, 1);
6468
}
6569

6670
</style>
6771

6872
<script>
6973

74+
// Clock Hands
75+
const secondHand = document.querySelector(".second-hand");
76+
const minuteHand = document.querySelector(".minute-hand");
77+
const hourHand = document.querySelector(".hour-hand");
78+
79+
function setDate(){
80+
81+
const now = new Date();
82+
83+
// Clock hand: Seconds
84+
const seconds = now.getSeconds();
85+
86+
// Formula: Divide seconds by 60 to get percent, multiply by 360 to get degrees, add 90 to offset the initial CSS transform of clock hands
87+
const secondsDegrees = ((seconds / 60) * 360) + 90;
88+
89+
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
90+
91+
// Clock hand: Minutes
92+
const minutes = now.getMinutes();
93+
const minutesDegrees = ((minutes / 60) * 360) + 90;
94+
95+
minuteHand.style.transform = `rotate(${minutesDegrees}deg)`;
96+
97+
// Clock hand: Hour
98+
const hours = now.getHours();
99+
const hoursDegrees = ((hours / 12) * 360) + 90;
100+
101+
hourHand.style.transform = `rotate(${hoursDegrees}deg)`;
102+
103+
104+
}
105+
106+
setInterval(setDate, 1000);
70107

71108
</script>
72109
</body>

0 commit comments

Comments
 (0)