js实现页面动态时间,滚动效果(年月日,小时、分钟、秒,星期,毫秒)

  • 年月日放在一个div里面;
  • 小时、分钟、秒放在一个div里面;
  • 星期放在一个div里面;
  • 毫秒放在一个div里面。
    因为它们的时间设置不同或img类不同

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>js制作一个动态页面时间,滚动效果</title>
    <style type="text/css">
        .clock{
            width: 520px;
            height: 520px;
            border-radius: 50%;
            margin: 0 auto;
            margin-top: 100px;
            background: url(images/bg.png) no-repeat ;
            position: relative;
        }

        .title{
            width: 195px;
            height: 45px;
            background: url(images/title.png) no-repeat;
            position: absolute;
            top: 110px;
            left: 160px;
        }


        .ymd{
            position: absolute;
            top: 190px;
            left: 70px;
        }

        .hms{
            position: absolute;
            top: 250px;
            left: 138px;
        }

        .millis{
            position: absolute;
            top: 310px;
            left: 190px;
            padding: 5px 30px;
            border: 3px solid #f00;
        }

        .day{
            position: absolute;
            top: 390px;
            left: 210px;
        }
    </style>
</head>
<body>
    <div class="clock">
        <div class="title"></div>
        <div class="box">
            <div class="ymd">
                <!--年-->
                <img src="images/0.png" > 
                <img src="images/0.png" >
                <img src="images/0.png" >
                <img src="images/0.png" >
                <!--图标:-->
                <span style="display: inline-block; width: 38px; height: 40px; background: url(images/year.png) no-repeat;"></span>

                <!--月-->
                <img src="images/0.png" > 
                <img src="images/0.png" >
                <!--图标:-->
                <span style="display: inline-block; width: 38px; height: 40px; background: url(images/month.png) no-repeat;"></span>

                <!--日-->
                <img src="images/0.png" > 
                <img src="images/0.png" >
                <!--图标:-->
                <span style="display: inline-block; width: 38px; height: 40px; background: url(images/00.png) no-repeat;"></span>
            </div>  

            <div class="hms">
                <!--时-->
                <img src="images/0.png" > 
                <img src="images/0.png" >
                <!--图标:-->
                <span style="display: inline-block; width: 25px; height: 37px; background: url(images/sign.png) no-repeat;"></span>

                <!--分-->
                <img src="images/0.png" > 
                <img src="images/0.png" >
                <!--图标:-->
                <span style="display: inline-block; width: 25px; height: 37px; background: url(images/sign.png) no-repeat;"></span>

                <!--秒-->
                <img src="images/0.png" > 
                <img src="images/0.png" >
                <!--图标:-->
            </div>  
        </div>

        <!--毫秒-->
        <div class="millis">
            <img src="images/0.png" > 
            <img src="images/0.png" >
            <img src="images/0.png" > 
        </div>

        <!--周-->
        <div class="day">
            <span style="display: inline-block; width: 78px; height: 38px; background: url(images/week.png) no-repeat;"></span>
            <img src="images/00.png"  />
        </div>
    </div>
</body>
</html>

javascript

<script type="text/javascript">
//  判断时间的位数
    function toNum(num){
        if (num<10){return '0'+num;}
        else{return ''+num;}
    }   

    (function(){
        var imgs=document.querySelectorAll('.box img'); 
        var imgsD=document.querySelectorAll('.day img');
        setInterval(function(){
//          获取当前时间
            var now = new Date();
            var year = now.getFullYear();
            var month = now.getMonth()+1;
            var date = now.getDate();
            var hours = now.getHours();
            var minutes = now.getMinutes();
            var seconds = now.getSeconds();

//          星期与其它时间分开写
            var day = now.getDay();
            imgsD[0].src='images/0'+day+'.png'; 

//          获取除星期、毫秒的时间字符串
            var now1=toNum(year)+toNum(month)+toNum(date)+toNum(hours)+toNum(minutes)+toNum(seconds);

//          除星期时间,时间是什么就显示图片是什么
            for(var i=0;i<imgs.length;i++){
                imgs[i].src='images/'+now1[i]+'.png'; 
            }

        },1000)

        //          毫秒
            setInterval(function(){
                var now = new Date();
                var millis = now.getMilliseconds();
                var imgsM=document.querySelectorAll('.millis img');
                imgsM[0].src='images/'+parseInt(millis%1000/100)+'.png';
                imgsM[1].src='images/'+parseInt(millis%1000%100/10)+'.png';
                imgsM[2].src='images/'+parseInt(millis%1000%100%10)+'.png';
                },100)
    })();
</script>

效果图
这里写图片描述

材料图
这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值