Native JS to achieve digital table special effects

Native JS to achieve digital table special effects

This article shares a digital clock effect implemented with native JS. The effect is as follows:

The numbers above are generated using the following 10 images:

The implementation code is as follows, you are welcome to copy and paste.

<!DOCTYPE html>
<html>
 
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Native JS to achieve digital table special effects</title>
    <style>
        body {
            background: blue;
            color: white;
            font-size: 30px;
        }
 
        #div1 {
            width: 220px;
            height: 36px;
            position: fixed;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            border: 1px #FFF solid;
        }
 
        #div1 img {
            width: 25px;
            height: 36px;
            position: relative;
        }
    </style>
    <script>
        window.onload = function () {
            //Get all the pictures var aImg = document.getElementsByTagName('img');
            //Get the local time object var oDate = new Date();
            //Hours + minutes + seconds var prevStr = toZero(oDate.getHours()) +
                toZero(oDate.getMinutes()) +
                toZero(oDate.getSeconds());
            //Declare the string variable for the next time var nextStr = '';
            //The number of seconds that change at the same time may be multiple digits, so create an array to store it. var arr = [];
            var timer = null;
 
            //The src address of the i-th img picture is the picture name corresponding to the i-th digit of the current time under img //104604=>Get the characters from 1 to 6 respectively through charAt(i), that is, 1, 0, 4, 6, 0, 4
            for (var i = 0; i < aImg.length; i++) {
                aImg[i].src = 'images/' + prevStr.charAt(i) + '.png';
            };
 
            //Set the timer to execute the toChange method once every 1 second setInterval(toChange, 1000);
 
            //Get the next time function toChange() {
                //Get the local time object var oDate = new Date();
                //Hours + minutes + seconds nextStr = toZero(oDate.getHours()) +
                    toZero(oDate.getMinutes()) +
                    toZero(oDate.getSeconds());
                //Compare the previous time with the next time toCom(prevStr, nextStr);
                //Assign the next 1 second to the current time, and keep changing prevStr = nextStr;
            };
            //Compare the last time with the next time function toCom(str1, str2) {
                // Before comparison, clear the previous value and reassign arr = [];
                //Traverse each digit of the first time for (var i = 0; i < str1.length; i++) {
                    //If one character is different from the character corresponding to the next time if (str1.charAt(i) != str2.charAt(i)) {
                        //Push it into the array arr.push(i);
                    }
                }
                //Execute image flip startMove();
 
            };
            //Image flip function function startMove() {
                //Image height 36px, set the second reduction to -4px
                var iSpeed ​​= -4;
 
                //Set the timer timer = setInterval(function () {
                    //Loop through the strings in the array whose last and next times have changed for (var i = 0; i < arr.length; i++) {
                        //If the image height of the name corresponding to the different characters in the array is equal to 0
                        if (aImg[arr[i]].offsetHeight == 0) {
                            //Set the change speed to increase by 4px
                            iSpeed ​​= 4;
                            //The image position corresponding to the name of different characters is equal to the image name corresponding to the i-th digit of the next time in img //i represents the digit where the two time changes are located, arr[i] gets the specific number aImg[arr[i]].src = 'images/' + nextStr.charAt(arr[i]) + '.png';
                        }
                        //The height of the picture corresponding to the name of the different characters in the array is equal to its content height minus 4px each time
                        //Note the height of style.height and offsetHeight aImg[arr[i]].style.height = aImg[arr[i]].offsetHeight + iSpeed ​​+ 'px';
                        //The top value of the picture corresponding to the name of the different characters in the array is equal to half of its content height minus 18px
                        //To ensure that each time the picture changes, it can be displayed in the center aImg[arr[i]].style.top = aImg[arr[i]].offsetHeight / 2 - 18 + 'px';
 
                        //When the image height reaches the maximum height of 36pxif (aImg[arr[i]].offsetHeight == 36) {
                            //Clear timer clearInterval(timer);
                        }
                    }
 
                }, 10);
            };
 
 
            //Get the local time. If the unit digit is less than 10, and the tens digit is 0, there is no such thing. Here is the encapsulated function toZero(num) {
                if (num < 10) {
                    return '0' + num;
                } else {
                    return '' + num;
                }
            }
        };
    </script>
</head>
 
<body>
    <div id="div1">
        <img src="images/0.png" />
        <img src="images/0.png" />:
        <img src="images/0.png" />
        <img src="images/0.png" />:
        <img src="images/0.png" />
        <img src="images/0.png" />
    </div>
</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:
  • JavaScript to achieve a simple digital clock
  • Realizing digital clock effect based on JavaScript

<<:  Analysis of the process of deploying Python applications in Docker containers

>>:  Summary of several error logs about MySQL MHA setup and switching

Recommend

In-depth explanation of binlog in MySQL 8.0

1 Introduction Binary log records SQL statements ...

Summarize some general principles of web design and production

<br />Related articles: 9 practical suggesti...

Detailed explanation of common methods of JavaScript String

Table of contents 1. charAt grammar parameter ind...

HTML introductory tutorial HTML tag symbols quickly mastered

Side note <br />If you know nothing about HT...

JavaScript Basics Series: Functions and Methods

Table of contents 1. The difference between funct...

Detailed explanation of basic operation commands for Linux network settings

Table of contents View network configuration View...

Detailed analysis of the difference between Ref and Reactive in Vue3.0

Table of contents Ref and Reactive Ref Reactive T...

How to install vim editor in Linux (Ubuntu 18.04)

You can go to the Ubuntu official website to down...

Detailed explanation of MYSQL stored procedure comments

Table of contents 1. Instructions for use 2. Prep...

Implementation of socket options in Linux network programming

Socket option function Function: Methods used to ...

JavaScript implementation of drop-down list

This article example shares the specific code of ...

How to install and configure ftp server in CentOS8.0

After the release of CentOS8.0-1905, we tried to ...

Detailed explanation of Vue's TodoList case

<template> <div id="root"> ...