温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用JavaScript怎么实现一个数字滑动时钟

发布时间:2021-04-09 18:11:39 阅读:181 作者:Leah 栏目:web开发
前端开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

使用JavaScript怎么实现一个数字滑动时钟?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style type="text/css">
    body,ul{margin:0;padding0;}
    .content{margin:100px auto;width1000px;}
    .box{position: relative;float: left;width50px;height120px;overflow: hidden;}
    .box li{position: absolute;left0;width:100%;height120px;list-style:none;font-size:80px;font-weight: bold;background-color#fffline-height120pxtext-align: center;}
    .colon{float: left;height120px;font-size80px;color#e91e63;font-weight: bold;line-height100px;}
  </style>
</head>
<body>
  <div class="content">
  </div>
  <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 = "<div class='box' id='box"+num+"'><ul>"
        this.num = num;
        num++;
        for(var i =0,l=timeNum[this.num];i<l;i++){
          innerHTML += "<li style='color:"+colors[i]+"'>"+i+"</li>";
        }
        innerHTML += "</ul><div>"
        content.innerHTML += innerHTML;
        if(num==2||num==4){content.innerHTML += "<div class='colon'>:</div>"}
      },
      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>

看完上述内容,你们掌握使用JavaScript怎么实现一个数字滑动时钟的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×