Skip to content

Commit c8ef5d1

Browse files
Madison DicksonMadison Dickson
authored andcommitted
failed attempt at making the 360 rotation look smooth
1 parent 7409a1a commit c8ef5d1

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

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

Lines changed: 49 additions & 4 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 hour-hand no-transition"></div>
13+
<div class="hand min-hand no-transition"></div>
14+
<div class="hand second-hand" style=""></div>
1515
</div>
1616
</div>
1717

@@ -62,12 +62,57 @@
6262
background:black;
6363
position: absolute;
6464
top:50%;
65+
transform-origin: 100%;
66+
transform:rotate(90deg);
67+
transition: all 0.5s;
68+
transition-timing-function: cubic-bezier(0.04, 1.77, 0.32, 0.94);
69+
}
70+
hand.no-transition {
71+
transition:none;
6572
}
66-
6773
</style>
6874

6975
<script>
76+
const hourhand = document.querySelector('.hour-hand')
77+
const minutehand = document.querySelector('.min-hand')
78+
const secondhand = document.querySelector('.second-hand')
79+
80+
function setHands(h,m,s){
81+
secondhand.style.transform = `rotate(${s/60 * 360 + 90}deg)`;
82+
minutehand.style.transform = `rotate(${m/60 * 360 + 90}deg)`;
83+
hourhand.style.transform = `rotate(${h/12 * 360 + 90}deg)`;
84+
85+
console.log(s, `rotate(${s/60 * 360 + 90}deg)`)
86+
}
87+
88+
function setTime() {
89+
const now = new Date();
90+
let seconds = now.getSeconds();
91+
const minutes = now.getMinutes();
92+
const hours = now.getHours();
93+
94+
//fix the weird slide transition from 359degrees -> 0degrees
95+
if(seconds === 0){
96+
seconds = 60;
97+
}
98+
99+
setHands(hours,minutes,seconds)
100+
101+
//tried, doesn't work.. still animates
102+
if(seconds === 60){
103+
console.log('add class')
104+
secondhand.style.transition = 'all 0s';
105+
setHands(hours,minutes,0)
106+
console.log('remove class')
107+
secondhand.style.transition = 'all 0.5s';
108+
// window.setTimeout( () => {
109+
// // secondhand.classList.remove('no-transition')
110+
// }, 0)
111+
}
112+
}
70113

114+
setTime();
115+
window.setInterval(setTime, 1000);
71116

72117
</script>
73118
</body>

0 commit comments

Comments
 (0)