登录  /  注册
首页 > web前端 > js教程 > 正文

JavaScript如何实现数字滑动时钟效果的实例

黄舟
发布: 2017-08-13 10:29:24
原创
1238人浏览过

javascript如何实现数字滑动时钟效果的实例

<!DOCTYPE html>
<html>
<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>
    <div>
    </div>
    <script type="text/javascript">
    (function(){
        var colors = ["#69D2E7", "#E20049", "#19215E", "#F38630", "#FA6900", "#FF4E50", "#F9D423", "#FFB6FF", "#474168", "#6ACAFC"];
        var content = document.querySelector(&#39;.content&#39;);
        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 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><div>"
                content.innerHTML += innerHTML;
                if(num==2||num==4){content.innerHTML += "<div>:</div>"}
            },
            dominit : function(){
                this.Ali = [].slice.call(document.getElementById(&#39;box&#39;+this.num).getElementsByTagName(&#39;li&#39;),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如何实现数字滑动时钟效果的实例的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号