JavaScript实现春节倒计时

运行结果:

在这里插入图片描述

完整代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .maxbox {
            width: 640px;
            height: 400px;
            margin: 200px auto;
            background-image: url(./3.jpg);
            background-size: 640px 400px;
            color: yellow;
            position: relative;

        }

        #tian {
            width: 57px;
            height: 32px;
            font-size: 26px;
            position: absolute;
            left: 66px;
            top: 27px;
            background-color: black;
            text-align: center;
            line-height: 32px;
            border-radius: 17px;
        }

        #shi {
            width: 38px;
            height: 32px;
            position: absolute;
            left: 210px;
            top: 8px;
            font-size: 26px;
            background-color: black;
            border-radius: 50%;
            text-align: center;
            line-height: 32px;
        }

        #fen {
            width: 38px;
            height: 32px;
            position: absolute;
            left: 332px;
            top: 29px;
            font-size: 26px;
            background-color: black;
            border-radius: 50%;
            text-align: center;
            line-height: 32px;
        }

        #miao1 {
            width: 38px;
            height: 32px;
            position: absolute;
            left: 467px;
            top: 6px;
            font-size: 26px;
            background-color: black;
            border-radius: 50%;
            text-align: center;
            line-height: 32px;
        }
    </style>
</head>
<body>
    <div class="maxbox">
        <div id="tian"></div>
        <div id="shi"></div>
        <div id="fen"></div>
        <div id="miao1"></div>
    </div>
    <script>
        var box = document.querySelector('.maxbox');
        var tian = document.getElementById("tian");
        var shi = document.getElementById("shi");
        var fen = document.getElementById("fen");
        var maio1 = document.getElementById("maio1");

        function daojishi() {
            var nowdate = new Date(); //获取当前的时间
            var chunjie = new Date("2022 / 2 / 1 "); //获取春节的时间
            chunjie.setHours(0, 0)
            var num = chunjie.getTime() - nowdate.getTime(); //获取时间差
            //获取天数
            tiantime = Math.floor(num / 86400 / 1000);
            // 获取小时数
            xiaoshi = Math.floor((num % (86400 * 1000)) / 3600 / 1000)
            console.log(xiaoshi);
            xiaoshi = xiaoshi < 10 ? '0' + xiaoshi : xiaoshi;
            // 获取分钟数
            fenzhong = Math.floor((num % (86400 * 1000 * 3600)) / 60 / 1000) % 60
            console.log(fenzhong);
            fenzhong = fenzhong < 10 ? '0' + fenzhong : fenzhong;
            // 获取秒
            miao = Math.floor(num / 1000) % 60
            console.log(miao);
            miao = miao < 10 ? '0' + miao : miao;
            //给i为shi的sapn添加定时小时内容
            tian.innerHTML = `${tiantime}天`
            shi.innerHTML = `${xiaoshi}`
            fen.innerHTML = `${fenzhong}`
            miao1.innerHTML = `${miao}`
        }
        setInterval(daojishi, 1000)
    </script>
</body>

</html>
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值