I am building a css3 clock and applied a transition to its second hand but it circles back at 0deg? I tried to add a one-time event listener to remove transition when it reaches 360 degrees in order for the second hand to jump instantly from 360 to the start, but it did not work. Is there any way to correct this, except incrementing the degrees infinitely.
var secHand = document.querySelector('div div:first-child');
var minHand = document.querySelector('div:nth-child(2)');
var hourHand = document.querySelector('div:nth-child(3)');
var myVar = setInterval(myTimer, 1000);
function myTimer() {
var d = new Date();
function updateHands() {
secHand.style.transform = `rotate(${d.getSeconds()*6}deg)`;
minHand.style.transform = `rotate(${d.getMinutes()*6}deg)`;
hourHand.style.transform = `rotate(${d.getHours()*(360/12) + d.getMinutes()*6*6/360}deg)`;
}
// I tried this but I don't know how to make it work properly.
if (d.getSeconds() == 0) {
secHand.style.transform = 'rotate(360deg)';
secHand.addEventListener('transitionend', function(e){
e.target.removeEventListener(e.type, arguments.callee);
this.style.transition = 'transform 0s';
updateHands();
this.style.transition = 'transform 0.5s';
})
}
else updateHands();
}
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
background: url('https://stmed.net/sites/default/files/sky-wallpapers-28043-2711012.jpg'), linear-gradient(to bottom, #1e528e 0%, #265889 50%, #9da671 100%);
background-repeat: no-repeat;
background-attachment: fixed;
}
div {
border-radius: 50%;
}
body > div {
border: 20px solid white;
width: 300px;
height: 300px;
border-radius: 50%;
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
margin: auto;
}
div div:first-child {
margin: auto;
width: 3px;
background-color: black;
height: 49%;
position: absolute;
left: 0;
right: 0;
bottom: 50%;
transform: rotate(180deg);
transform-origin: 50% 100%;
transition: transform 0.5s;
transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1) !important;
}
div:nth-child(2) {
width: 5px;
background-color: black;
height: 46%;
position: absolute;
left: 0;
right: 0;
bottom: 50%;
margin: auto;
transform: rotate(360deg);
transform-origin: 50% 100%;
transition: transform 2s;
}
div:nth-child(3) {
width: 5px;
background-color: black;
height: 43%;
position: absolute;
left: 0;
right: 0;
bottom: 50%;
margin: auto;
transform: rotate(360deg);
transform-origin: 50% 100%;
transition: transform 3s;
}
<div>
<div></div>
<div></div>
<div></div>
</div>