Analog Clock In Dark UI

Syiainfoku make a Analog Clock In Dark UI

Analog Clock In Dark UI

Hello everyone, I am the founder of Syiainfoku. Today in Syiainfoku you will learn how to create a Analog Clock With Vanilla JavaScript.

Analog Clock is also a JavaScript Mini Project, and very useful to train you, if you are just starting to learn JS. And many people suggest you to practice creating some Mini Projects JS so that you understand JS better.

Preview Analog Clock In Dark UI

Preview Analog Clock In Dark UI

Source Code and Brief Explanation

To make this program [Analog Clock with Vanilla JavaScript]. First you need to create three Files, one HTML File and one CSS File and lastly one JS File. After creating these files, just paste the following code into your file. You can also download the source code file of this Analog Clock from the given download button.

First, create an HTML file with the name index.html and paste the given code in your HTML file. Remember, you must create a file with an .html extension. 

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Analog Clock Dark UI | hitoricoding</title>
<link rel="stylesheet" href="./assets/style.css">
</head>

<body>
<div class="clock">
<div class="hour">
<div class="hr" id="hr"></div>
</div>
<div class="minute">
<div class="mn" id="mn"></div>
</div>
<div class="second">
<div class="sc" id="sc"></div>
</div>
</div>
</body>

<script src="./assets/main.js" charset="utf-8"></script>

</html>

Second, create a CSS file with the name style.css and paste the given code in your CSS file. Remember, you must create a file with a .css extension.

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #08091f;
}

.clock {
width: 350px;
height: 350px;
display: flex;
justify-content: center;
align-items: center;
background: url(../assets/clock.png);
background-size: cover;
border: 4px solid #0c0b2d;
border-radius: 50%;
box-shadow: 0 -15px 15px rgba(255, 255, 255, 0.09),
inset 0 -15px 15px rgba(255, 255, 255, 0.09),
0 15px 15px rgba(0, 0, 0, 0.3),
inset 0 15px 15px rgba(0, 0, 0, 0.3);
}

.clock::before {
content: '';
position: absolute;
width: 15px;
height: 15px;
background: #fff;
border-radius: 50%;
z-index: 10000;
}

.clock .hour,
.clock .minute,
.clock .second {
position: absolute;
}

.clock .hour,
.hr {
width: 160px;
height: 160px;
}

.clock .minute,
.mn {
width: 190px;
height: 190px;
}

.clock .second,
.sc {
width: 230px;
height: 230px;
}

.hr,
.mn,
.sc {
display: flex;
justify-content: center;
border-radius: 50%;
position: absolute;
}

.hr::before {
content: '';
position: absolute;
width: 8px;
height: 80px;
background: #ff0053;
z-index: 10;
border-radius: 6px 6px 0 0;
}

/* ff0053 */

.mn::before {
content: '';
position: absolute;
width: 4px;
height: 90px;
background: #f5f31d;
z-index: 11;
border-radius: 6px 6px 0 0;
}

.sc::before {
content: '';
position: absolute;
width: 2px;
height: 150px;
background: #319bd0;
z-index: 12;
border-radius: 6px 6px 0 0;
}

Finally, create a JS file with the name main.js and paste the given code in your JS file. Remember, you must create a file with a .js extension.

const deg = 6;
const hr = document.querySelector('#hr');
const mn = document.querySelector('#mn');
const sc = document.querySelector('#sc');

setInterval(() => {
let day = new Date();
let hh = day.getHours() * 30;
let mm = day.getMinutes() * deg;
let ss = day.getSeconds() * deg;

hr.style.transform = `rotateZ(${(hh)+(mm/12)}deg)`;
mn.style.transform = `rotateZ(${mm}deg)`;
sc.style.transform = `rotateZ(${ss}deg)`;
});

That's it, now you have successfully created a Analog Clock with Vanilla JavaScript, If your code is not working or you are facing any error/problem, please download the source code file from the given download button. It's free and a .zip file will be downloaded then you have to extract it. 

Closing

Thank you for those of you who have read and downloaded this source code, hopefully it can be useful and add to your insight.

If you found this article useful, you can share it. That's all from me, and THANK YOU