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

Ubuntu16.04 installation mysql5.7.22 graphic tutorial

VMware12.0+Ubuntu16.04+MySQL5.7.22 installation t...

Detailed steps for QT to connect to MYSQL database

The first step is to add the corresponding databa...

React implements the addition, deletion, modification and query of todolist

Table of contents Take todolist as an example The...

Two methods to stretch the background image of a web page

There are two solutions: One is CSS, using backgro...

Docker deployment RabbitMQ container implementation process analysis

1. Pull the image First, execute the following co...

Detailed explanation of mkdir command in Linux learning

Table of contents Preface 1. Basic knowledge of f...

Solve the problem of Mac Docker x509 certificate

question Recently I needed to log in to a private...

MySQL scheduled database backup operation example

This article describes the example of MySQL sched...

How to deploy nextcloud network disk using docker

NextCloud You can share any files or folders on y...

Analysis of the advantages of path.join() in Node.js

You might be wondering why you should use the pat...

How to install MySQL 5.7 on Ubuntu and configure the data storage path

1. Install MySQL This article is installed via AP...

Detailed explanation of Strict mode in JavaScript

Table of contents Introduction Using Strict mode ...

Vue implements a small weather forecast application

This is a website I imitated when I was self-stud...

How to use time as a judgment condition in MySQL

Background: During the development process, we of...

Examples of optimistic locking and pessimistic locking in MySQL

The task of concurrency control in a database man...