Home > Web Front-end > JS Tutorial > body text

Detailed explanation of js digital sliding clock steps

php中世界最好的语言
Release: 2018-04-19 10:28:34
Original
992 people have browsed it

This time I will bring you a detailed explanation of the steps of the js digital sliding clock. What are the precautions for implementing the js digital sliding clock? The following is a practical case, let's take a look.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style type="text/css">
    body,ul{margin:0;padding: 0;}
    .content{margin:100px auto;width: 1000px;}
    .box{position: relative;float: left;width: 50px;height: 120px;overflow: hidden;}
    .box li{position: absolute;left: 0;width:100%;height: 120px;list-style:none;font-size:80px;font-weight: bold;background-color: #fff; line-height: 120px; text-align: center;}
    .colon{float: left;height: 120px;font-size: 80px;color: #e91e63;font-weight: bold;line-height: 100px;}
  </style>
</head>
<body>
  <p class="content">
  </p>
  <script type="text/javascript">
  (function(){
    var colors = ["#69D2E7", "#E20049", "#19215E", "#F38630", "#FA6900", "#FF4E50", "#F9D423", "#FFB6FF", "#474168", "#6ACAFC"];
    var content = document.querySelector('.content');
    var num = 0;
    var height = 120;
    var maxheight = (2-num)*height;
    var timeNum = [3,10,6,10,6,10];
    var position = [];
    var NumberBoxs = [];
    for(var i =0;i<10;i++){
      position.push((1-i)*height);
    };
    function NumberBox() {}
    NumberBox.prototype = {
      init : function () {
        var innerHTML = "<p class=&#39;box&#39; id=&#39;box"+num+"&#39;><ul>"
        this.num = num;
        num++;
        for(var i =0,l=timeNum[this.num];i<l;i++){
          innerHTML += "<li style=&#39;color:"+colors[i]+"&#39;>"+i+"</li>";
        }
        innerHTML += "</ul><p>"
        content.innerHTML += innerHTML;
        if(num==2||num==4){content.innerHTML += "<p class=&#39;colon&#39;>:</p>"}
      },
      dominit : function(){
        this.Ali = [].slice.call(document.getElementById('box'+this.num).getElementsByTagName('li'),0);    
        this.Ali.forEach(function(dom,i){
          dom.style.top = position[i] + "px";
          dom.style.transition = "top .8s";
        })  
        this.hasdom = true;  
      },
      toNum : function (n) {
        if(!this.hasdom){this.dominit();}
        n = ""+n;
        var p = this;
        var l = p.Ali.length-1;
        while(p.Ali[1].innerHTML!=n){
        p.Ali.unshift(p.Ali.pop());
        }
        p.Ali.forEach(function (dom,i) {
        dom.style.zIndex = (i==l)?"-1":"1";
        dom.style.top = position[i] + "px";
        })     
      }
    }
    for(var i=0;i<6;i++){
      var o = new NumberBox();
      o.init();
      NumberBoxs.push(o);
    }
    function getTime() {
      var time = new Date();
      return (""+Fixed2(time.getHours())+Fixed2(time.getMinutes())+Fixed2(time.getSeconds())).split("");
    }
    function Fixed2(n){
      return Number(n)<10?"0"+n:n;
    }
    (function () {
      var time = getTime();
      NumberBoxs.forEach(function(obj,i){
        obj.toNum(time[i]);
      });
      setTimeout(arguments.callee,1000);
    })()
  })();
  </script>
</body>
</html>
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the use of Vue scope slots

JS implementation of data validation and check box form submission

How to add Vue0.1 code to Vue2.0 for use

The above is the detailed content of Detailed explanation of js digital sliding clock steps. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!