A Pen by Khalid Saifullah Fuad on CodePen.
Created
October 15, 2021 17:04
-
-
Save KhalidSaifullahFuad/c5e9382d841d43dfe3d54a2706afd61b to your computer and use it in GitHub Desktop.
Analog Clock with JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="clock-body"> | |
<div class="hours"></div> | |
<div class="disc disc-top"></div> | |
<div class="disc disc-bottom"></div> | |
<div class="hand second"></div> | |
<div class="hand minute"></div> | |
<div class="hand hour"></div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const time = document.querySelector(".hours"); | |
const secHand = document.querySelector(".second"); | |
const minHand = document.querySelector(".minute"); | |
const hourHand = document.querySelector(".hour"); | |
for (let i = 1; i <= 60; i++) { | |
if (i % 5 == 0) { | |
time.innerHTML += "<div class='hour-number'><div>" + (i / 5) + "</div></div>"; | |
let hours = document.getElementsByClassName("hour-number"); | |
hours[hours.length - 1].style.transform = `translateX(-50%) rotate(${i*6}deg)`; | |
hours[hours.length - 1].firstChild.style.transform = `rotate(${i*-6}deg)`; | |
} else { | |
time.innerHTML += "<div class='minute-bar'></div>"; | |
let bars = document.getElementsByClassName("minute-bar"); | |
bars[bars.length - 1].style.transform = `translateX(-50%) rotate(${i*6}deg)`; | |
} | |
} | |
function startClock() { | |
const now = new Date(); | |
const seconds = now.getSeconds(); | |
const minutes = now.getMinutes(); | |
const hours = now.getHours(); | |
let secDeg = seconds * (360 / 60) + minutes * 360; | |
let minDeg = minutes * (360 / 60) + seconds / 12; | |
let hourDeg = hours * (360 / 12) + (minutes / 12) * (360 / 60); | |
secHand.style.transform = `translateX(-50%) rotate(${secDeg}deg)`; | |
minHand.style.transform = `translateX(-50%) rotate(${minDeg}deg)`; | |
hourHand.style.transform = `translateX(-50%) rotate(${hourDeg}deg)`; | |
} | |
setInterval(startClock, 1000); | |
startClock(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import url("https://fonts.googleapis.com/css2?family=Open+Sans:wght@700&display=swap"); | |
:root { | |
--hand-width: 0.5rem; | |
--sec-hand: 6rem; | |
--min-hand: calc(var(--sec-hand) * 0.85); | |
--hour-hand: calc(var(--sec-hand) * 0.6); | |
--clr-red: hsl(11, 79%, 49%); | |
--clr-dark: hsl(0, 0%, 19%); | |
} | |
* { | |
margin: 0; | |
padding: 0; | |
box-sizing: border-box; | |
} | |
body { | |
height: 100vh; | |
width: 100vw; | |
display: grid; | |
place-items: center; | |
font-family: "Open Sans", sans-serif; | |
} | |
.clock-body { | |
height: 15rem; | |
width: 15rem; | |
border-radius: 50%; | |
position: relative; | |
box-shadow: inset 0px 0px 30px #a6a9b4, 0 0 0 15px #333; | |
} | |
.disc { | |
width: 0.5rem; | |
height: 0.5rem; | |
background: var(--clr-red); | |
border-radius: 50%; | |
z-index: 2; | |
position: absolute; | |
top: 50%; | |
left: 50%; | |
z-index: 2; | |
transform: translateX(-50%) translateY(-50%); | |
} | |
.disc-bottom { | |
width: 0.9rem; | |
height: 0.9rem; | |
background: #333; | |
z-index: 1; | |
} | |
.hand { | |
background: var(--clr-dark); | |
border-radius: 15px 15px 0 0; | |
position: absolute; | |
bottom: 50%; | |
left: 50%; | |
transform-origin: bottom; | |
transition: all 0.05s; | |
transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1); | |
} | |
.second { | |
background: var(--clr-red); | |
width: 0.1rem; | |
height: var(--sec-hand); | |
transform: translateX(-50%) rotate(0deg); | |
z-index: 2; | |
} | |
.second::before { | |
content: ""; | |
height: 1.5rem; | |
width: 0.1rem; | |
background: var(--clr-red); | |
position: absolute; | |
top: 100%; | |
} | |
.second::after { | |
content: ""; | |
height: 1.5rem; | |
width: 0.3rem; | |
background: var(--clr-red); | |
position: absolute; | |
top: 120%; | |
transform: translateX(-35%); | |
} | |
.minute { | |
height: var(--min-hand); | |
width: 0.3rem; | |
transform: translateX(-50%) rotate(0deg); | |
background: var(--clr-dark); | |
} | |
.minute::after { | |
content: ""; | |
height: 1.2rem; | |
width: 0.3rem; | |
background: var(--clr-dark); | |
position: absolute; | |
top: 100%; | |
border-radius: 0px 0px 10px 10px; | |
} | |
.hour { | |
height: var(--hour-hand); | |
width: 0.4rem; | |
background: var(--clr-dark); | |
transform: translateX(-50%) rotate(0deg); | |
z-index: -1; | |
} | |
.hour::after { | |
content: ""; | |
height: 1.2rem; | |
width: 0.4rem; | |
background: var(--clr-dark); | |
position: absolute; | |
top: 100%; | |
border-radius: 0px 0px 10px 10px; | |
} | |
.hour-number { | |
position: absolute; | |
left: 50%; | |
height: 7.5rem; | |
transform-origin: bottom; | |
padding: 1rem; | |
color: var(--clr-dark); | |
user-select: none; | |
font-size: 1.4rem; | |
} | |
.minute-bar { | |
position: absolute; | |
left: 50%; | |
width: 1rem; | |
height: 7.5rem; | |
transform-origin: bottom; | |
transform: rotate(20deg) translateX(-50%); | |
} | |
.hour-number::before, | |
.minute-bar::before { | |
content: ""; | |
position: absolute; | |
left: 50%; | |
right: 50%; | |
top: 0px; | |
border-radius: 0px 0px 15px 15px; | |
transform: translateX(-50%); | |
background: var(--clr-dark); | |
} | |
.hour-number::before { | |
height: 10px; | |
width: 4px; | |
} | |
.minute-bar::before { | |
height: 6px; | |
width: 2px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment