Analog Clock Using HTML, CSS and JS


Copy HTMl Code:

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheethref="style.css">
<meta charset="UTF-8">
<meta name="viewportcontent="width=device-width, initial-scale=1.0"> 
<title>Analog Clock Using JS</title>
</head>
<body>
<div class="clock">
<div class="hrid="hr"></div>
<div class="mnid="mn"></div>
<div class="scid="sc"></div>
</div>
</body>
<script src="main.js"></script>
</html>



Copy CSS code:
*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
body{
    justify-content: center;
    display: flex;
    align-items: center;
    min-height: 100vh;
}
.clock{
    justify-content: center;
    display: flex;
    align-items: center;
    height: 450px;
    width: 450px;
    border-radius: 50%;
    border: 20px solid #062c2d;
    background-image: url(clock.png);
    background-size: cover;
  box-shadow: inset 0 0 30px #062c2d,
                inset 0 20px 20px rgba(0, 0, 0, 0.2),
                inset 0 0 0 4px rgba(255, 255, 255, 1);

}
.clock::before{
    height: 15px;
    width: 15px;
    position: absolute;
    border: 2px solid #062c2d;
    background-color: #062c2d;
    content: "";
    z-index: 10000;
    border-radius: 50%;

}
.hr, .mn, .sc{
    display: flex;
    justify-content: center;
    position: absolute;
    border-radius: 50%;
}
.hr{
    height: 160px;
    width: 160px;
}
.mn{
    height: 200px;
    width: 190px;
}
.sc{
    height: 240px;
    width: 230px;
}

.hr::before{
    content: "";
    position: absolute;
    height: 80px;
    width: 8px;
    background-color: rgb(0, 38, 235);
    border-radius: 10px 10px 0 0;
}
.mn::before{
    content: "";
    position: absolute;
    height: 100px;
    width: 4px;
    background-color: rgb(0, 235, 39);
    border-radius: 10px 10px 0 0;
}
.sc::before{
    content: "";
    position: absolute;
    height: 120px;
    width: 2px;
    background-color: rgb(253, 0, 55);
    border-radius: 10px 10px 0 0;
}

Copy JS code:
setInterval(() => {
    d = new Date();
    htime = d.getHours();
    mtime = d.getMinutes();
    stime = d.getSeconds();
    hrotation = 30*htime + mtime/2;
    mrotation = 6*mtime;
    srotation = 6*stime;
    hr.style.transform = `rotate(${hrotation}deg)`;
    mn.style.transform = `rotate(${mrotation}deg)`;
    sc.style.transform = `rotate(${srotation}deg)`;
}, 1000);




OUTPUT:



Comments