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

Docker View Process, Memory, and Cup Consumption

Docker view process, memory, cup consumption Star...

Example of integrating Kafka with Nginx

background nginx-kafka-module is a plug-in for ng...

How to encapsulate axios request with vue

In fact, it is very simple to encapsulate axios i...

Installation steps of docker-ce on Raspberry Pi 4b ubuntu19 server

The Raspberry Pi model is 4b, 1G RAM. The system ...

Detailed explanation of how to create an updateable view in MySQL

This article uses an example to describe how to c...

Vue implements pull-down to load more

Developers familiar with Element-UI may have had ...

Using front-end HTML+CSS+JS to develop a simple TODOLIST function (notepad)

Table of contents 1. Brief Introduction 2. Run sc...

A tutorial on how to install, use, and automatically compile TypeScript

1. Introduction to TypeScript The previous articl...

How to use ssh tunnel to connect to mysql server

Preface In some cases, we only know the intranet ...

Talking about ContentType(s) from image/x-png

This also caused the inability to upload png files...

How to change the password of mysql5.7.20 under linux CentOS 7.4

After MySQL was upgraded to version 5.7, its secu...

The actual process of encapsulating axios in the project

Table of contents Preface Benefits of axios encap...

Promise encapsulation wx.request method

The previous article introduced the implementatio...

Teach you how to subcontract uniapp and mini-programs (pictures and text)

Table of contents 1. Mini Program Subcontracting ...