Skip to content

Commit 4b64d1a

Browse files
committed
done clock completely
1 parent b7d2b71 commit 4b64d1a

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

02 - JS + CSS Clock/Memo.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
1. CSS clock hand:
2+
- use transform: rotate() in CSS file
3+
- by default the object rotates in the middle of the object: use transform-origin: x%; to move the origin on the X axis.
4+
- set default rotate at 90° to have them face upwards
5+
- set a transition for the animation not to be too hard: transition: all 0.5s;
6+
- use a transition-timing-funtion to update the way the object moves
7+
8+
2. JS:
9+
- setInterval to set the interval at which the hands should move
10+
- A function to set the date. You have a date you find which second you are currently in then you transform into degress (make sure that you add 90° to the total because we added that to the CSS)
11+
- then you add the style transform rotate by variable degrees.
12+

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,44 @@
6161
background:black;
6262
position: absolute;
6363
top:50%;
64+
transform-origin: 100%;
65+
transform: rotate(90deg);
66+
transition: all 0.1s;
67+
transition-timing-function: cubic-bezier(0.54, 2.52, 0.96, 1.09);
6468
}
6569

6670
</style>
6771

6872
<script>
73+
const sec_hand = document.querySelector('.second-hand')
74+
const min_hand = document.querySelector('.min-hand')
75+
const hour_hand = document.querySelector('.hour-hand')
76+
77+
78+
function setDate() {
79+
var date = new Date();
80+
81+
// SECONDS
82+
var seconds = date.getSeconds();
83+
var secondsDegrees = ((seconds / 60) * 360) + 90;
84+
sec_hand.style.transform = `rotate(${secondsDegrees}deg)`;
85+
86+
// MINUTES
87+
var minutes = date.getMinutes();
88+
var minutesDegrees = ((minutes / 60) * 360) + 90;
89+
min_hand.style.transform = `rotate(${minutesDegrees}deg)`;
90+
91+
// HOUR
92+
var hour = date.getHours();
93+
var hourDegrees = ((hour / 24) * 360) + 90;
94+
hour_hand.style.transform = `rotate(${hourDegrees}deg)`;
95+
96+
}
97+
98+
99+
100+
window.setInterval(setDate, 1000);
101+
69102

70103

71104
</script>

0 commit comments

Comments
 (0)