JavaScript to achieve digital clock effects

JavaScript to achieve digital clock effects

This article example shares the specific code for implementing digital clock effects in javascript for your reference. The specific content is as follows

See the effect first, dynamic digital clock

jQuery is used, but only to get elements, only a little bit

Object-oriented development

Look at the code

HTML, import jQuery and js yourself, jQuery first

<body>
    <div class="wrapper">
        <div class="column">
            <!-- This div represents the tens digit, which can be 0, 1, or 2. -->
            <div>0</div>
            <div>1</div>
            <div>2</div>
        </div>
        <!-- The following content is too redundant to be written in HTML code, so it is written using js-->
        <div class="column ten"></div>
        <div class="coln">:</div>
        <div class="column six"></div>
        <div class="column ten"></div>
        <div class="coln">:</div>
        <div class="column six"></div>
        <div class="column ten"></div>
    </div>
</body>

CSS

*{
     margin: 0;
     padding: 0;
}
html,body{
     height: 100%;
     width: 100%;
     background-color: #0e141b;
    overflow: hidden;
    /* Set overflow to hide */
}
.wrapper{
    text-align: center;
    width: 100%;
}
.wrapper .column,
.wrapper .coln{
    display: inline-block;
    vertical-align: top;
    color: rgba(224,230,235,0.89);
    font-size: 86px;
    line-height: 86px;
    font-weight: 300;
}
.column{
    transition: all 300ms ease-in;
}
.coln{
    /* Colon position*/
    transform: translateY(calc(50vh - 83px));
}
/* The following are the transparency corresponding to different class names*/
.visible{
    opacity: 1;
}
.close{
    opacity: 0.25;
}
.far{
    opacity: 0.15;
}
.distance{
    opacity: 0.05;
}

JS

function Index(dom, use) {
    // Convert the passed DOM element to an array this.column = Array.from(dom);
    // Transfer use to the global state, this is to determine whether the time system to be displayed is 112 hours or 24 hours this.use = use;
    // This array is the class name to be set later this.classList = ['visible', 'close', 'far', 'distance', 'distance', 'distance', 'distance', 'distance'];
    this.createDom();
    this.start();//Start}
// Start function Index.prototype.start = function () {
    var self = this;
    setInterval(function () {
        var c = self.getClock();
        // console.log(c);
        self.column.forEach(function (ele, index) {
            var n = + c[index];
            var offset = n * 86; //moving distance console.log(offset);
            $(ele).css({
                'transform': 'translateY(calc(50vh - ' + offset + 'px - 73px))'
                // Set movement });
            Array.from(ele.children).forEach(function (ele2, index2) {
                var className = self.getClass(n, index2);
                // Call function to set class name $(ele2).attr('class', className);
            })
        })
    }, 500);
};
// Set different class names for elements with different distance times Index.prototype.getClass = function (n, i) {
    var className = this.classList.find(function (ele, index) {
        return i - index === n || i + index === n;
    })
    return className || "";
}
 
// Get the time and format it, string 21:06:09 ==> 210609
Index.prototype.getClock = function () {
    var d = new Date();
    // Here we use a ternary operator. If use is true, we get the value. If it is false, we convert the remainder of 12 to a 12-hour system. return [this.use ? d.getHours() : d.getHours() % 12 || 12, d.getMinutes(), d.getSeconds()].reduce(function (p, n) {
        return p + ('0' + n).slice(-2);
        // Here we add 0 to the single digit, for example, 1 plus 0 gives 01, if 12 plus 0 gives 012, but taking the last two digits of the value gives 12
    }, '')
};
// Since writing all HTML elements into the HTML file is too redundant, we use a for loop to add them Index.prototype.creatDom = function () {
    for (var i = 0; i < 6; i++) {
        var oDiv = '<div>' + i + '</div>';
        $(".six").append(oDiv);
    }
    for (var i = 0; i < 10; i++) {
        var iDiv = '<div>' + i + '</div>';
        $(".ten").append(iDiv);
    }
};
 
// The second parameter, true is 24-hour system, false is 12-hour system new Index($('.column'), true);

I wrote a relatively complete comment for js, so you should be able to understand it.

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 implement dynamic digital clock
  • JavaScript to achieve digital clock effect
  • Vue.js implements a digital clock function example with date and week
  • JS+CSS to achieve scrolling digital clock effect
  • js realizes a simple digital clock effect
  • Use JS to display the countdown digital clock effect
  • JavaScript digital clock example to achieve scrolling effect
  • Web page countdown digital clock effect implemented by JS
  • JavaScript digital clock example sharing
  • HTML5 canvas js (digital clock) example code

<<:  How to change the root user's password in MySQL

>>:  How to connect to a remote server and transfer files via a jump server in Linux

Recommend

Continuous delivery using Jenkins and Docker under Docker

1. What is Continuous Delivery The software produ...

Detailed explanation of Vue element plus multi-language switching

Table of contents Preface How to switch between m...

Detailed installation tutorial of mysql 5.7 under CentOS 6 and 7

You always need data for development. As a server...

Python Flask WeChat applet login process and login api implementation code

1. Let’s take a look at the effect first Data ret...

MySQL optimization: use join instead of subquery

Use JOIN instead of sub-queries MySQL supports SQ...

RHCE installs Apache and accesses IP with a browser

1. at is configured to write "This is a at t...

Detailed explanation of mysql5.6 master-slave setup and asynchronous issues

Table of contents 1. MySQL master-slave replicati...

Detailed steps for setting up host Nginx + Docker WordPress Mysql

environment Linux 3.10.0-693.el7.x86_64 Docker ve...

Tips for List Building for Website Maintenance Pages

And, many times, maintenance requires your website...

MySQL REVOKE to delete user permissions

In MySQL, you can use the REVOKE statement to rem...

Mysql timeline data to obtain the first three data of the same day

Create table data CREATE TABLE `praise_info` ( `i...

Chrome monitors cookie changes and assigns values

The following code introduces Chrome's monito...

Introduction to html form control disabled attributes readonly VS disabled

There are two ways to disable form submission in ...

Web Design Experience: Efficiently Writing Web Code

Originally, this seventh chapter should be a deep ...