Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4d4d59c

Browse files
author
Stefan Bauckmeier
committed
02 done
1 parent 363fd2a commit 4d4d59c

File tree

1 file changed

+48
-31
lines changed

1 file changed

+48
-31
lines changed

02 - JS + CSS Clock/index.html

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@
55
<title>JS + CSS Clock</title>
66
</head>
77
<body>
8-
9-
10-
<div class="clock">
11-
<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>
15-
</div>
8+
<div class="clock">
9+
<div class="clock-face">
10+
<div class="hand hour-hand"></div>
11+
<div class="hand min-hand"></div>
12+
<div class="hand second-hand"></div>
1613
</div>
17-
14+
</div>
1815

1916
<style>
2017
html {
@@ -56,41 +53,61 @@
5653
}
5754

5855
.hand {
56+
position: absolute;
5957
width:50%;
6058
height:6px;
61-
background:black;
62-
position: absolute;
6359
top:50%;
60+
background:black;
6461
transform-origin: 100%;
65-
transform: rotate(90deg);
6662
transition: all 0.05s;
6763
transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1);
6864
}
69-
</style>
7065

71-
<script>
72-
const secondHand = document.querySelector('.second-hand');
73-
const minsHand = document.querySelector('.min-hand');
74-
const hourHand = document.querySelector('.hour-hand');
66+
.hand.hour-hand {
67+
width:30%;
68+
left: 20%;
69+
}
70+
.hand.min-hand {
71+
width:40%;
72+
left: 10%;
73+
}
74+
75+
76+
</style>
7577

76-
function setDate() {
77-
const now = new Date();
78+
<script>
79+
class Clock {
80+
constructor() {
81+
let klass = this;
7882

79-
const seconds = now.getSeconds();
80-
const secondsDegrees = ((seconds / 60) * 360) + 90;
81-
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
83+
this.secondHand = document.querySelector('.hand.second-hand');
84+
this.minuteHand = document.querySelector('.hand.min-hand');
85+
this.hourHand = document.querySelector('.hand.hour-hand');
8286

83-
const mins = now.getMinutes();
84-
const minsDegrees = ((mins / 60) * 360) + 90;
85-
minsHand.style.transform = `rotate(${minsDegrees}deg)`;
87+
this.redraw();
88+
window.setInterval(function() { klass.redraw() }, 1000);
89+
}
8690

87-
const hour = now.getMinutes();
88-
const hourDegrees = ((mins / 12) * 360) + 90;
89-
hourHand.style.transform = `rotate(${hourDegrees}deg)`;
90-
}
91+
redraw() {
92+
let time = new Date();
9193

92-
setInterval(setDate, 1000);
94+
this.setHandTo(this.secondHand, 360 / 60 * time.getSeconds());
95+
this.setHandTo(this.minuteHand, 360 / 60 * time.getMinutes());
96+
this.setHandTo(this.hourHand, 360 / 12 * time.getHours());
97+
}
98+
99+
setHandTo(handElement, degree) {
100+
// initial state is on 45min (270deg), so we have to adopt the rotation to this
101+
let fixedDegree = 90 + degree;
102+
if (fixedDegree >= 360) {
103+
fixedDegree = fixedDegree - 360;
104+
}
105+
106+
handElement.setAttribute('style', `transform: rotate(${fixedDegree}deg)`);
107+
}
108+
}
93109

94-
</script>
110+
new Clock();
111+
</script>
95112
</body>
96113
</html>

0 commit comments

Comments
 (0)