Implementing a simple web clock with JavaScript

Implementing a simple web clock with JavaScript

Use JavaScript to implement a web page clock. The effect is shown in the figure below:

First, complete the loading of the resources of the dial and pointer in the body:

<div><img src="../../image/clockface.jpg" alt=""></div>
<hr id="hour" >
<hr id="min">
<hr id="second">

Set CSS style:

<style>
        body{
            margin: 0;
        }
        div{
            margin: 0 auto;
            width: 600px;
            height: 600px;
        }
        #hour{
            background-color: black;
            width: 130px;
            height: 10px;
            position: fixed;
            top: 295px;
            left: 50%;
            margin-left: -65px;
        }
        #min{
            background-color: red;
            width: 200px;
            height: 8px;
            position: fixed;
            top: 296px;
            left: 50%;
            margin-left: -100px;
        }
        #second{
            background-color: yellow;
            width: 270px;
            height: 5px;
            position: fixed;
            top: 297.5px;
            left: 50%;
            margin-left: -135px;
        }
</style>

Finally, the JS code part uses the loop timer setInterval() to call the main function once a second. In the main function, new Date() is used to create a time object. .getHours(); .getMinutes(); .getSeconds() are used to obtain the current hours, minutes, and seconds. Then, the CSS built-in animation-rotation is used to change the angle of the pointer:

setInterval(watch,1000);
var anjleSeconds=0,anjleMin=0,anjleHours=0;
function watch() {
        var Time = new Date();
        anjleSeconds=Time.getSeconds()/60*360+90;
        anjleMin=Time.getMinutes()/60*360+90;
        anjleHours=nowHours/12*360+90;
        document.getElementById("second").style.transform="rotate("+anjleSeconds+"deg)";
        document.getElementById("min").style.transform="rotate("+anjleMin+"deg)";
        document.getElementById("hour").style.transform="rotate("+anjleHours+"deg)";
    }

The current problem is that the hour, minute, and second hands are the same length at both ends because they are represented by the hr tag.

The complete code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body{
            margin: 0;
        }
        div{
            margin: 0 auto;
            width: 600px;
            height: 600px;
        }
        #hour{
            background-color: black;
            width: 130px;
            height: 10px;
            position: fixed;
            top: 295px;
            left: 50%;
            margin-left: -65px;
        }
        #min{
            background-color: red;
            width: 200px;
            height: 8px;
            position: fixed;
            top: 296px;
            left: 50%;
            margin-left: -100px;
        }
        #second{
            background-color: yellow;
            width: 270px;
            height: 5px;
            position: fixed;
            top: 297.5px;
            left: 50%;
            margin-left: -135px;
        }
    </style>
</head>
<body>
<div><img src="../../image/clockface.jpg" alt=""></div>
<hr id="hour" >
<hr id="min">
<hr id="second">
<script>
    setInterval(watch,1000);
    var anjleSeconds=0,anjleMin=0,anjleHours=0;
    function watch() {
        var Time = new Date();
        anjleSeconds=Time.getSeconds()/60*360+90;
        anjleMin=Time.getMinutes()/60*360+90;
        anjleHours=Time.getHours()/12*360+90;
        document.getElementById("second").style.transform="rotate("+anjleSeconds+"deg)";
        document.getElementById("min").style.transform="rotate("+anjleMin+"deg)";
        document.getElementById("hour").style.transform="rotate("+anjleHours+"deg)";
    }
</script>
</body>
</html>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • JS realizes web clock special effects
  • js+html5 implements canvas drawing web clock method
  • Web page countdown digital clock effect implemented by JS
  • Implementing a simple web clock using JavaScript

<<:  Automatically build and deploy using Docker+Jenkins

>>:  The best explanation of HTTPS

Recommend

4 Scanning Tools for the Linux Desktop

While the paperless world has not yet emerged, mo...

Example of how to set div background transparent

There are two common ways to make div background ...

Detailed explanation of the API in Vue.js that is easy to overlook

Table of contents nextTick v-model syntax sugar ....

Image hover toggle button implemented with CSS3

Result:Implementation Code html <ul class=&quo...

HTML pop-up div is very useful to realize mobile centering

Copy code The code is as follows: <!DOCTYPE ht...

MySQL 8.0.11 Installation Guide for Mac

MAC installs mysql8.0, the specific contents are ...

The pitfall record of the rubber rebound effect of iOS WeChat H5 page

Business requirements One of the projects I have ...

How to solve the error "ERROR 1045 (28000)" when logging in to MySQL

Today, I logged into the server and prepared to m...

Detailed explanation of the available environment variables in Docker Compose

Several parts of Compose deal with environment va...

Tutorial on how to quickly deploy a Nebula Graph cluster using Docker swarm

1. Introduction This article describes how to use...

How to use the yum command

1. Introduction to yum Yum (full name Yellow dogU...

Detailed process of installing logstash in Docker

Edit docker-compose.yml and add the following con...

MySQL group query optimization method

MySQL handles GROUP BY and DISTINCT queries simil...

VUE implements a Flappy Bird game sample code

Flappy Bird is a very simple little game that eve...

How to write transparent CSS for images using filters

How to write transparent CSS for images using filt...