Skip to content

Commit 6f0e8a1

Browse files
committed
Day 2 - JS Clock + "turnover" fix
1 parent 2e903a1 commit 6f0e8a1

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,61 @@
6161
background:black;
6262
position: absolute;
6363
top:50%;
64+
transform-origin: 100%;
65+
transform: rotate(90deg);
66+
transition-timing-function: cubic-bezier(0.42, 0, 0.55, 1.46);
67+
border-radius: 25%;
68+
}
69+
70+
.tick-transition{
71+
transition: all 0.5s;
72+
}
73+
74+
.hand.second-hand{
75+
background: lightgreen;
76+
}
77+
78+
.hand.min-hand{
79+
background: yellow;
80+
width: 40%;
81+
right:50%; /*this allows us to make the hand smaller*/
82+
}
83+
84+
.hand.hour-hand{
85+
background: magenta;
86+
width: 33%;
87+
right:50%; /*this allows us to make the hand smaller*/
6488
}
6589

6690
</style>
6791

6892
<script>
93+
const secondHand = document.querySelector(".second-hand");
94+
const minuteHand = document.querySelector(".min-hand");
95+
const hourHand = document.querySelector(".hour-hand");
96+
function setDate(){
97+
const now = new Date();
98+
99+
// Calculate total rotation to avoid jumping back when it rolls over to the next value
100+
const seconds = now.getSeconds() + now.getMinutes()*60 + now.getHours()*60*60;
101+
const secondsDegrees = 90 + (seconds / 60)*360;
102+
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
103+
104+
const minute = now.getMinutes() + now.getHours()*60;
105+
const minuteDegrees = 90 + (minute / 60)*360;
106+
minuteHand.style.transform = `rotate(${minuteDegrees}deg)`;
107+
108+
const hour = now.getHours();
109+
const hourDegrees = 90 + (hour / 12)*360;
110+
hourHand.style.transform = `rotate(${hourDegrees}deg)`;
111+
}
69112

113+
// Initially get everyone in place
114+
setDate();
115+
// Turn on transitions
116+
document.querySelectorAll(".hand").forEach(hand => hand.classList.add("tick-transition"));
117+
// Start ticking
118+
setInterval(setDate, 1000);
70119

71120
</script>
72121
</body>

0 commit comments

Comments
 (0)