Just a quick intro, I created an animated clock using CSS and HTML for homework, but now I want to enhance it by adding JavaScript functionality. I tried to sync the clock hands with the current time using JS, but for some reason, the animation always starts with the hands at the 12 o'clock position (its default state):
let seconds = document.getElementById("seconds");
let hours = document.getElementById("hours");
let minutes = document.getElementById("minutes");
let date = new Date();
let dateMinutes = date.getMinutes();
let dateSeconds = date.getSeconds();
let dateHours = date.getHours();
let currentHour = dateHours + date.getTimezoneOffset();
console.log(dateHours, dateMinutes, dateSeconds);
seconds.style.animationPlayState = "paused";
minutes.style.animationPlayState = "paused";
hours.style.animationPlayState = "paused";
if (currentHour > 12) {
currentHour = currentHour / 2;
}
let percentHours = (360 / 43200) * (currentHour * 3600);
let percentMin = dateMinutes * 6;
let percentSec = dateSeconds * 6;
seconds.style.transform = "rotateZ(" + percentSec + "deg)";
minutes.style.transform = "rotateZ(" + percentMin + "deg)";
hours.style.transform = "rotateZ(" + percentHours + "deg)";
seconds.style.mozTransform = "rotateZ(" + percentSec + "deg)";
minutes.style.mozTransform = "rotateZ(" + percentMin + "deg)";
hours.style.mozTransform = "rotateZ(" + percentHours + "deg)";
seconds.style.webkitTransform = "rotate(" + percentSec + "deg)";
minutes.style.webkitTransform = "rotate(" + percentMin + "deg)";
hours.style.webkitTransform = "rotate(" + percentHours + "deg)";
seconds.style.animationPlayState = "running";
minutes.style.animationPlayState = "running";
hours.style.animationPlayState = "running";
#clock {
width: 400px;
height: 400px;
margin: 20px auto;
border: 15px solid rgba(29, 29, 29, 1);
border-radius: 50%;
position: relative;
background-color: rgba(117, 141, 163, 0.37);
background-image: url(clock2.png);
background-size: contain;
background-repeat: no-repeat;
}
#seconds {
position: absolute;
width: 2px;
height: 180px;
border-top: 2px solid #131111;
border-left: 2px solid #131111;
border-right: 2px solid #131111;
border-radius: 4px;
background-color: rgb(255, 28, 17);
top: 20px;
left: 197px;
animation: rotarSeg 60s infinite;
animation-timing-function: steps(60);
transform-origin: bottom center;
}
#minutes {
position: absolute;
width: 4px;
height: 160px;
right: 196px;
border-top: 4px solid #131111;
border-left: 2px solid #131111;
border-right: 2px solid #131111;
border-radius: 50%;
background-color: rgb(246, 248, 248);
margin-top: 38px;
animation: rotarSeg 3600s infinite;
animation-timing-function: steps(60);
transform-origin: bottom center;
}
#hours {
position: absolute;
width: 6px;
height: 120px;
top: 74px;
left: 193px;
border-top: 6px solid #131111;
border-left: 4px solid #131111;
border-right: 4px solid #131111;
border-radius: 50%;
background-color: rgba(255, 246, 193, 0.705);
animation: rotar 43200s infinite linear;
transform-origin: bottom center;
}
Any suggestions as to why the HTML file doesn't work when executed on a server?