Home >Web Front-end >JS Tutorial >Build a Analog Clock Website
Hello, fellow developers! Today, I'm thrilled to share a project I recently completed: an Analog Clock. This project is a visually appealing and interactive way to display time using a traditional analog clock face. It's an excellent project for honing your JavaScript, CSS, and HTML skills, particularly in working with animations, DOM manipulation, and time-based functions. Whether you're a beginner looking to practice or an experienced developer wanting to create a classic clock interface, this project is a great choice.
The Analog Clock is a real-time clock that mimics the appearance and functionality of a traditional analog clock. The clock dynamically updates every second, with the hour, minute, and second hands rotating smoothly to reflect the current time. This project is ideal for developers who want to practice building dynamic and visually appealing web applications.
Here's a quick look at the project structure:
Analog-Clock/ ├── index.html ├── style.css └── script.js
To get started with the project, follow these steps:
Clone the repository:
git clone https://github.com/abhishekgurjar-in/Analog-Clock.git
Open the project directory:
cd Analog-Clock
Run the project:
The index.html file sets up the structure of the webpage, including the clock container and the header. Below is a snippet of the HTML code:
8b05045a5be5764f313ed5b9168a17e6 49099650ebdc5f3125501fa170048923 93f0f5c25f18dab9d176bd4f6de5d30e 7c8d9f814bcad6a1d7abe4eda5f773e5 26faf3d1af674280d03ba217d87e9421 b2386ffb911b14667cb8f0f91ea547a7Analog Clock6e916e0f7d1e588d4f442bf645aedb2f af75c476cdb7e6c074ca6da9b40841de 90392ec4442ad9ff612213ec639da4832cacc6d41bbb37262a98f745aa00fbf0 9c3bca370b5104690d9ef395f2c5f8d1 6c04bd5ca3fcae76e30b72ad730ca86d 924ff17625d603f964501dd897c96cc6 4a249f0d628e2318394fd9b75b4636b1Analog Clock473f0a7621bec819994bb5020d29372a 16b28748ea4df4d9c2150843fecfba68 79794d8004b182431dd9645149aae01b ae3ec3e20c207b48c6a71e25c698c4a416b28748ea4df4d9c2150843fecfba68 639d12f8b0b9d4dac7618dcf544a40a216b28748ea4df4d9c2150843fecfba68 ae78b0f312e3cd17c752800dca089cb816b28748ea4df4d9c2150843fecfba68 16b28748ea4df4d9c2150843fecfba68 ffd6ba4147bda351239915f463e46e38 e388a4556c0f65e1904146cc1a846beeMade with ❤️ by Abhishek Gurjar94b3e26ee717c64999d7867364b1b4a3 16b28748ea4df4d9c2150843fecfba68 36cc49f0c466276486e50c850b7e4956 73a6ac4ed44ffec12cee46588e518a5e
The style.css file styles the clock's container and hands, ensuring they rotate correctly to display the time. Key styles include:
#clockContainer { position: relative; margin: auto; height: 30vw; width: 30vw; background: url(clock.png) no-repeat; background-size: 100%; } #hour, #minute, #second { position: absolute; background: black; border-radius: 10px; transform-origin: bottom; } #hour { width: 1.8%; height: 25%; top: 25%; left: 48.85%; opacity: 0.8; } #minute { width: 1.6%; height: 30%; top: 19%; left: 48.9%; opacity: 0.8; } #second { width: 1%; height: 40%; top: 9%; left: 49.25%; opacity: 0.8; } .header { margin: 80px; text-align: center; } .footer { margin: 50px; text-align: center; }
The script.js file handles the calculation of the current time and updates the rotation of the clock's hands accordingly. Below is a snippet of the JavaScript code:
setInterval(() => { const date = new Date(); const hourTime = date.getHours(); const minuteTime = date.getMinutes(); const secondTime = date.getSeconds(); const hourRotation = 30 * hourTime + minuteTime / 2; const minuteRotation = 6 * minuteTime; const secondRotation = 6 * secondTime; const hour = document.getElementById('hour'); const minute = document.getElementById('minute'); const second = document.getElementById('second'); hour.style.transform = `rotate(${hourRotation}deg)`; minute.style.transform = `rotate(${minuteRotation}deg)`; second.style.transform = `rotate(${secondRotation}deg)`; }, 1000);
You can check out the live demo of the Analog Clock here.
Building this Analog Clock was a rewarding experience that allowed me to delve deeper into JavaScript animations and DOM manipulation. I hope this project inspires you to create your own interactive and visually appealing applications. Feel free to explore the code, customize it, and use it in your own projects. Happy coding!
This project was inspired by the classic design of analog clocks and the need for a simple, real-time time display tool.
The above is the detailed content of Build a Analog Clock Website. For more information, please follow other related articles on the PHP Chinese website!