Hey Devmoodies, in this post, we will discuss how to make a live clock in HTML, CSS, and Javascript within 10 minutes. Below are the source codes and guidance on how t make a live clock just follow these simple steps to make your calculator.
- Open your favorite code editor (TRY THIS).
- Create three files and name them as follow:
- index.html
- style.css
- main.js
- Paste the below codes into these files
- Save all files.
- Open the index.html file in any browser.
- That's it :)
Table of Contents
Index.html
Paste the below code in the index.html file.
<html lang="en">
<head>
<meta charset="UTF-8"></meta>
<meta content="IE=edge" http-equiv="X-UA-Compatible"></meta>
<meta content="width=device-width, initial-scale=1.0" name="viewport"></meta>
<!-- title -->
<title>HTML Live Clock | Devmoods </title>
<!-- stylesheet -->
<link href="style.css" rel="stylesheet"></link>
</head>
<body>
<div class="clock">
<div>
<div class="info date"></div>
<div class="info day"></div>
</div>
<div class="dot"></div>
<div>
<div class="hour-hand"></div>
<div class="minute-hand"></div>
<div class="second-hand"></div>
</div>
<div>
<span class="h3">3</span>
<span class="h6">6</span>
<span class="h9">9</span>
<span class="h12">12</span>
</div>
<div class="diallines"></div>
</div>
<!-- script file -->
<script src="main.js"></script>
</body>
</html>
Style.css
Paste the below code in the style.css file.
.clock {
background: #ececec;
width: 300px;
height: 300px;
margin: 8% auto 0;
border-radius: 50%;
border: 14px solid #333;
position: relative;
box-shadow: 0 2vw 4vw -1vw rgba(0,0,0,0.8);
}
.dot {
width: 14px;
height: 14px;
border-radius: 50%;
background: #ccc;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
position: absolute;
z-index: 10;
box-shadow: 0 2px 4px -1px black;
}
.hour-hand {
position: absolute;
z-index: 5;
width: 4px;
height: 65px;
background: #333;
top: 79px;
transform-origin: 50% 72px;
left: 50%;
margin-left: -2px;
border-top-left-radius: 50%;
border-top-right-radius: 50%;
}
.minute-hand {
position: absolute;
z-index: 6;
width: 4px;
height: 100px;
background: #666;
top: 46px;
left: 50%;
margin-left: -2px;
border-top-left-radius: 50%;
border-top-right-radius: 50%;
transform-origin: 50% 105px;
}
.second-hand {
position: absolute;
z-index: 7;
width: 2px;
height: 120px;
background: gold;
top: 26px;
lefT: 50%;
margin-left: -1px;
border-top-left-radius: 50%;
border-top-right-radius: 50%;
transform-origin: 50% 125px;
}
span {
display: inline-block;
position: absolute;
color: #333;
font-size: 22px;
font-family: 'Poiret One';
font-weight: 700;
z-index: 4;
}
.h12 {
top: 30px;
left: 50%;
margin-left: -9px;
}
.h3 {
top: 140px;
right: 30px;
}
.h6 {
bottom: 30px;
left: 50%;
margin-left: -5px;
}
.h9 {
left: 32px;
top: 140px;
}
.diallines {
position: absolute;
z-index: 2;
width: 2px;
height: 15px;
background: #666;
left: 50%;
margin-left: -1px;
transform-origin: 50% 150px;
}
.diallines:nth-of-type(5n) {
position: absolute;
z-index: 2;
width: 4px;
height: 25px;
background: #666;
left: 50%;
margin-left: -1px;
transform-origin: 50% 150px;
}
.info {
position: absolute;
width: 120px;
height: 20px;
border-radius: 7px;
background: #ccc;
text-align: center;
line-height: 20px;
color: #000;
font-size: 11px;
top: 200px;
left: 50%;
margin-left: -60px;
font-family: "Poiret One";
font-weight: 700;
z-index: 3;
letter-spacing: 3px;
margin-left: -60px;
left: 50%;
}
.date {
top: 80px;
}
.day {
top: 200px;
}
Main.js
Paste the below code in the main.js file
var dialLines = document.getElementsByClassName('diallines');
var clockEl = document.getElementsByClassName('clock')[0];
for (var i = 1; i < 60; i++) {
clockEl.innerHTML += "<div class='diallines'></div>";
dialLines[i].style.transform = "rotate(" + 6 * i + "deg)";
}
function clock() {
var weekday = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
],
d = new Date(),
h = d.getHours(),
m = d.getMinutes(),
s = d.getSeconds(),
date = d.getDate(),
month = d.getMonth() + 1,
year = d.getFullYear(),
hDeg = h * 30 + m * (360/720),
mDeg = m * 6 + s * (360/3600),
sDeg = s * 6,
hEl = document.querySelector('.hour-hand'),
mEl = document.querySelector('.minute-hand'),
sEl = document.querySelector('.second-hand'),
dateEl = document.querySelector('.date'),
dayEl = document.querySelector('.day');
var day = weekday[d.getDay()];
if(month < 9) {
month = "0" + month;
}
hEl.style.transform = "rotate("+hDeg+"deg)";
mEl.style.transform = "rotate("+mDeg+"deg)";
sEl.style.transform = "rotate("+sDeg+"deg)";
dateEl.innerHTML = date+"/"+month+"/"+year;
dayEl.innerHTML = day;
}
setInterval("clock()", 100);
That's it! Now just save all files and open the index.html file in any browser and you are able to use your live clock :)
Original source ~ By Vasko Petrov (Analog Clock)
Post a Comment