JavaScript Analog Clock Widget

JavaScript Analog Clock Widget
Code Snippet:Such Clock, much wow
Author: Shawn
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 2,401
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code snippet helps you to create an analog clock widget to display the current time. It comes with a beautiful dial and shadow that makes it a realistic table clock. The clock interface is designed with HTML CSS without using images or canvas and functionalized with JavaScript.

You can easily integrate this clock widget anywhere on your website by placing its HTML code into a specific container. Similarly, you can customize the appearance of the clock with additional CSS.

How to Create Analog Clock Widget

1. First of all, create the HTML structure for the clock as follows:

 <div class="clock-body">
  <div class="clock">
    <div class="dails">
      <h2 class="hour-number twelve">12</h2>
      <h2 class="hour-number three">3</h2>
      <h2 class="hour-number six">6</h2>
      <h2 class="hour-number nine">9</h2>
      <div class="hour-dail"></div>
      <div class="minute-dail"></div>
      <div class="seconds-dail"></div>
      <div class="center-dail"></div>
    </div>
  </div>
</div>
<div class="clock-shadow"></div>

2. After that, style the clock using the following CSS styles. You can change the CSS values if you want to customize the clock’s appearance.

.clock-body{
    width: 550px;
    height: 550px;
    border-radius: 20%;
    background: tomato;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: inset 1px 10px 40px 40px  rgb(212, 40, 40);
    margin: 0 auto;
}
.clock{
    width: 400px;
    height: 400px;
    border-radius: 50%;
    background: rgb(60, 60, 78);
    border: 30px solid rgba(212, 40, 40, 1);
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: inset 5px 5px 50px 20px black;
}

.dails{
    width: 340px;
    height: 340px;
    background: rgba(66, 66, 105, 0.5);
    border-radius: 50%;
    position: relative;
    box-shadow: 5px 5px 30px 10px black;
    display: flex;
    justify-content: center;
    align-items: center;
}
.hour-dail{
    width: 1px;
    height: 100px;
    padding: 0 4px 0 4px;
    background: white;
    box-shadow: 3px 3px 10px black;
    position: absolute;
    top: 70px;
    left: 50%;
    transform: rotate(0deg) translateX(-50%);
    transform-origin: bottom center;
}
.minute-dail{
    width: 1px;
    height: 150px;
    padding: 0 4px 0 4px;
    background: white;
    box-shadow: 3px 3px 10px black;
    position: absolute;
    top: 20px;
    left: 50%;
    transform: rotate(0deg) translateX(-50%);
    transform-origin: bottom center;
}
.seconds-dail{
    width: 1px;
    height: 150px;
    padding: 0 1px 0 1px;
    background: white;
    box-shadow: 3px 3px 10px black;
    position: absolute;
    top: 20px;
    left: 50%;
    transform: rotate(0deg) translateX(-50%);
    transform-origin: bottom center;
    z-index: 1;
}
.center-dail{
    width: 40px;  
    height: 40px;
    background: white;
    border-radius: 50%;
    box-shadow: 2px 2px 10px black;
    z-index: 100;
    
}
.clock-shadow{
    width: 450px;
    height: 50px;
    border-radius: 50%;
    background: rgba(0, 0, 0, .2);
    margin-top: 1.5em;
    box-shadow: 0 0 5px 10px rgba(0, 0, 0, .2);
    margin-left: auto;
    margin-right: auto;
}
h2{
    background: linear-gradient(rgb(212, 40, 40), tomato);
    font-size: 2.8em;
    padding: 0;
    margin: 0;    
}

.hour-number{
    position: absolute;
    color: tomato;
    font-size: 4em;
    font-weight: bold;
    font-family: sans-serif;
    z-index: 0;
    background-clip: text;
    -webkit-background-clip: text;
    color: rgba(0, 0, 0, .2);
}
.twelve{
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
}
.three{
    top: 50%;
    right: 20px;
    transform: translateY(-50%);
}
.six{
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%); 
}
.nine{
    top: 50%;
    left: 20px;
    transform: translateY(-50%); 
}

3. Finally, add the following JavaScript code to your project to functionalize the analog clock.

const secDail = document.querySelector(".seconds-dail");
const minDail = document.querySelector(".minute-dail");
const hrsDail = document.querySelector(".hour-dail");

const setDate = () => {
  let now = new Date().toLocaleString("en-US", {
    timeZone: "Africa/Johannesburg"
  });
  now = new Date(now);
  const seconds = now.getSeconds();
  const minutes = now.getMinutes();
  const hours = now.getHours();
  const secondsDegrees = (seconds / 60) * 360;
  const minutesDegrees = (minutes / 60) * 360;
  const hoursDegrees = (hours / 12 ) * 360;
  secDail.style.transform = `rotate(${secondsDegrees}deg)`;
  minDail.style.transform = `rotate(${minutesDegrees}deg)`;
  hrsDail.style.transform = `rotate(${hoursDegrees}deg)`;
};
setInterval(setDate, 1000);

That’s all! hopefully, you have successfully integrated this JavaScript analog clock widget into your web/app project. If you have any questions or facing any issues, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X