Skip to content

Commit 64a927c

Browse files
committed
completed lesson 02 clock
1 parent f08dffa commit 64a927c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,49 @@
6161
background:black;
6262
position: absolute;
6363
top:50%;
64+
transform-origin: 100%;
65+
transform: rotate(90deg);
66+
transition: all 0.05s; /* I like .5s if the hand is not moving as far each time */
67+
transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1); /* There is a curve picker in Chrome dev tools. Sweet! */
6468
}
6569

6670
</style>
6771

6872
<script>
73+
const secondHand = document.querySelector(".second-hand");
74+
const minuteHand = document.querySelector(".min-hand");
75+
const hourHand = document.querySelector(".hour-hand");
6976

77+
function setDate(){
78+
const currentTime = new Date();
79+
80+
const seconds = currentTime.getSeconds();
81+
const secondsDegrees = (((seconds/60) * 360) + 90); // convert from minute time (60s)
82+
// to base ten (over 100), then multiple by degrees in a circle
83+
// and add 90 to account for the rotation we gave it by default in the CSS
84+
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
85+
86+
const minutes = currentTime.getMinutes();
87+
const minutesDegrees = (((minutes/60) * 360) + 90); // hand moves same distance as
88+
// for seconds, seconds and minutes share same stops on a clock
89+
minuteHand.style.transform = `rotate(${minutesDegrees}deg)`;
7090

91+
const hours = currentTime.getHours();
92+
const hoursDegrees = (((hours/12) * 360) + 90); // 12 stops on a clock for hours
93+
hourHand.style.transform = `rotate(${hoursDegrees}deg)`;
94+
}
95+
96+
97+
setInterval(setDate, 1000);
98+
99+
100+
/*
101+
new things I learned:
102+
- The CSS transform property. With it, elements can be translated, rotated, scaled, and skewed.
103+
- There is a cubic bezier editor in Chrome dev tools that lets you pick just the right curve. Add the transition-timing-function property to an element and then click on the small purple icon next to the value.
104+
- Practiced using some of the prototype methods for Date(), such as getSeconds().
105+
- Had to think about how a clock works and do a bit of math.
106+
*/
71107
</script>
72108
</body>
73109
</html>

0 commit comments

Comments
 (0)