架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 11039|回复: 0

[CSS/DIV] 跳动的电子时钟

[复制链接]
发表于 2014-11-14 18:13:17 | 显示全部楼层 |阅读模式
360软件小助手截图20141114181140.png
[mw_shl_code=css,true]<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="UTF-8">
        <title>电子时钟</title>
</head>
<style type="text/css">
        *{margin: 0;padding: 0;list-style: none}
        body{background-color: #D7154A;font-family: 'Microsoft Yahei'}
        .demo{width: 330px;height: 100px;border: 3px solid #fff;margin: 200px auto;line-height: 100px;text-align: center;color: #fff;font-size: 50px;font-weight: 600;position: relative;overflow: hidden;}
        .size{width: 100px;height: 100px;float: left;}
        .pointer{width: 10px;height: 100px;float: left;}
        .bottom{width: 200px;height: 100px;text-align: center; margin: 0 auto;color: #fff}

        #next-h{position: absolute;width: 100px;height: 100px;left: 0px;top: 100px;}        
        #next-m{position: absolute;width: 100px;height: 100px;left: 110px;top: 100px;}
        #next-s{position: absolute;width: 100px;height: 100px;left: 220px;top: 100px;}

</style>
<body>
        <div class="demo">
                <div id="hour" class="size"></div>
                <div id="next-h"></div>
                <div class="pointer">:</div>
                <div id="minute" class="size"></div>
                <div id="next-m"></div>
                <div class="pointer">:</div>
                <div id="second" class="size"></div>
                <div id="next-s"></div>
        </div>
        <div class="bottom">武软论坛</div>
</body>
<script type="text/javascript">
    //获取当前时间
        var date=new Date();
        var hour=date.getHours();
        var minute=date.getMinutes();
        var second=date.getSeconds();

        // 获取正在显示的时间的dom
        var ohour=document.getElementById("hour");
    var ominute=document.getElementById("minute");
    var osecond=document.getElementById("second");

    //时间初始化
    function init(){
            ohour.innerHTML=fill(hour);
            ominute.innerHTML=fill(minute);
            osecond.innerHTML=fill(second);
    }
    init();
   
    // 获取被隐藏的dom,这里的dom是为了显示接下来的时间
    var nhour=document.getElementById("next-h");
    var nminute=document.getElementById("next-m");
    var nsecond=document.getElementById("next-s");

    //定时器   控制时间的变化
    setInterval(function(){
       if(second==60){
                  second=0;
                  //分针数加1
                  if(minute==60){
                             minute=0;
                             //时针+1
                             if(hour==24){
                                        hour=0;
                             }
                          //显示在影藏的盒子中
                          nhour.innerHTML=fill(hour);
                          nhour++;
                          //调用浮动函数
                   move(nhour,ohour);
                   //盒子还要复位
                        nhour.style.top="100px";                             
                  }
                  
                  //显示在影藏的盒子中
                  nminute.innerHTML=fill(minute);
                  minute++;
                  //调用浮动函数
           move(nminute,ominute);
           //盒子还要复位
                nminute.style.top="100px";
       }else{
                  second++;
       }
       nsecond.innerHTML=fill(second);
    }, 1000)

   
    //下一秒dom向上运动
    //参数1:要运动的盒子   
    //参数2:最终显示时间的盒子
    function move(obj,show){
            var timer=setInterval(function(){
                obj.style.top=getStyle(obj,"top")-1+'px';
                if(getStyle(obj,"top")<=0){
                        clearInterval(timer);  
                show.innerHTML=obj.innerHTML;
                }
            }, 1)            
    }

    // 监听时间有没有变化
    var old=0;
    setInterval(function(){
        if(old!=nsecond.innerHTML){
                move(nsecond,osecond);
                nsecond.style.top="100px";
                old=nsecond.innerHTML;
        }
    }, 1)


    //辅助函数,获取属性值
        function getStyle(obj,style){
            var attr=window.getComputedStyle ? window.getComputedStyle(obj,null)[style]:obj.currentStyle[style];
            //去掉单位px
            attr=attr.slice(0, -2);
            return parseInt(attr);
        }            

        //如果时间的长度是1位,则在前面补0
        function fill(str){
                var s=str.toString();
        if(s.length==1){
                s='0'+s;
                return s;
        }else{
                return str;
        }
        }

</script>
</html>[/mw_shl_code]




上一篇:CSS3制作写轮眼(动画)
下一篇:黑客帝国:矩阵革命
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2025-6-18 07:52

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表