Skip to content

Commit 3ac088f

Browse files
authored
Update index-FINISHED.html
Few tweaks from the tutorial code. Added 'js-' prefix to classes that I used as JS hooks. Also added comments for things I may forget or didn't know.
1 parent 72379c3 commit 3ac088f

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

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

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
<div class="clock">
1111
<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>
12+
<div class="hand js-hour-hand"></div>
13+
<div class="hand js-minute-hand"></div>
14+
<div class="hand js-second-hand"></div>
1515
</div>
1616
</div>
1717

@@ -58,41 +58,49 @@
5858
.hand {
5959
width:50%;
6060
height:6px;
61-
background:black;
61+
background-color: black;
6262
position: absolute;
6363
top:50%;
64-
transform-origin: 100%;
65-
transform: rotate(90deg);
64+
transform-origin: 100%; /* make the right end the pivot point of rotation, not the middle (default = 50%) */
65+
transform: rotate(90deg); /* flip the hand from horizontal, to vertical pointing at 12:00; */
6666
transition: all 0.05s;
67-
transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1);
67+
transition-timing-function: ease-in-out; /* helps mimic second hand tick action */
68+
z-index: 25;
6869
}
69-
</style>
7070

71-
<script>
72-
const secondHand = document.querySelector('.second-hand');
73-
const minsHand = document.querySelector('.min-hand');
74-
const hourHand = document.querySelector('.hour-hand');
75-
76-
function setDate() {
77-
const now = new Date();
78-
79-
const seconds = now.getSeconds();
80-
const secondsDegrees = ((seconds / 60) * 360) + 90;
81-
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
82-
83-
const mins = now.getMinutes();
84-
const minsDegrees = ((mins / 60) * 360) + 90;
85-
minsHand.style.transform = `rotate(${minsDegrees}deg)`;
86-
87-
const hour = now.getHours();
88-
const hourDegrees = ((hour / 12) * 360) + 90;
89-
hourHand.style.transform = `rotate(${hourDegrees}deg)`;
90-
}
71+
.js-second-hand {
72+
background-color: red;
73+
height: 2px;
74+
z-index: 10;
75+
}
9176

92-
setInterval(setDate, 1000);
77+
</style>
78+
79+
<script>
80+
const secondHand = document.querySelector('.js-second-hand');
81+
const minuteHand = document.querySelector('.js-minute-hand');
82+
const hourHand = document.querySelector('.js-hour-hand');
83+
84+
function setDate() {
85+
const now = new Date();
86+
const seconds = now.getSeconds();
87+
const secondsDegrees = ((seconds / 60) * 360) + 90; /* for every second move the second hand 6 degrees */
88+
secondHand.style.transform = `rotate(${secondsDegrees}deg)`; /* */
89+
// console.log('seconds= ' + seconds);
90+
91+
const minutes = now.getMinutes();
92+
const minutesDegrees = ((minutes / 60) * 360) + 90;
93+
minuteHand.style.transform = `rotate(${minutesDegrees}deg)`;
94+
// console.log('minutes= ' + minutes);
95+
96+
const hours = now.getHours();
97+
const hoursDegrees = ((hours / 12) * 360) + 90;
98+
hourHand.style.transform = `rotate(${hoursDegrees}deg)`;
99+
// console.log('hours= ' + hours);
100+
}
93101

94-
setDate();
102+
setInterval(setDate, 1000); /* run function every 1 second */
95103

96-
</script>
104+
</script>
97105
</body>
98106
</html>

0 commit comments

Comments
 (0)