Skip to content

Commit e35764e

Browse files
committed
implemented clock
1 parent 6bdb671 commit e35764e

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,47 @@
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.58, 1);
6468
}
6569

6670
</style>
6771

6872
<script>
73+
const secondHand = document.querySelector('.second-hand');
74+
const minsHand = document.querySelector('.min-hand');
75+
const hourHand = document.querySelector('.hour-hand');
76+
var trackingSecond = 0;
6977

78+
function setDate() {
79+
// grab the date
80+
const now = new Date();
81+
const seconds = now.getSeconds();
82+
83+
// fixed the weird animation when second is 0 (0-59)
84+
if (seconds === 0) {
85+
trackingSecond = trackingSecond + 1;
86+
}
87+
88+
const secondsDegrees = (((seconds + trackingSecond * 60) / 60) * 360) + 90;
89+
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
90+
91+
const mins = now.getMinutes();
92+
const minsDegress = ((mins / 60) * 360) + 90;
93+
minsHand.style.transform = `rotate(${minsDegress}deg)`;
94+
95+
96+
const hour = now.getHours();
97+
const hourDegrees = ((hour / 12) * 360) + 90;
98+
hourHand.style.transform = `rotate(${hourDegrees}deg)`;
99+
}
100+
101+
//run every second
102+
setInterval(setDate, 1000);
103+
// set immediately
104+
setDate();
70105

71106
</script>
72107
</body>

0 commit comments

Comments
 (0)