计时器的书写

对于很多网页和很多生活中的地方,都会用到计时器,那么这个准确来计时的东西是怎么写出来的呢,今天带大家写一个简单的计时器。

书写css和html的内容

<h1 class="title">距离2021高考,还有</h1>

    <div class="time-item">
        <span><span id="day">00</span></span>
        <strong><span id="hour">00</span></strong>
        <strong><span id="minute">00</span></strong>
        <strong><span id="second">00</span></strong>
    </div>
 * {
            margin: 0;
            padding: 0;
        }
        
        .time-item {
            width: 450px;
            height: 45px;
            margin: 0 auto;
        }
        
        .time-item strong {
            background: orange;
            color: #fff;
            line-height: 49px;
            font-size: 36px;
            font-family: Arial;
            padding: 0 10px;
            margin-right: 10px;
            border-radius: 5px;
            box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.2);
        }
        
        .time-item>span {
            float: left;
            line-height: 49px;
            color: orange;
            font-size: 32px;
            margin: 0 10px;
            font-family: Arial, Helvetica, sans-serif;
        }
        
        .title {
            width: 330px;
            height: 50px;
            margin: 200px auto 50px auto;
        }

将天,时,分,秒分开为4个不同的内容在这里插入图片描述
没有js的内容时显示如上

添加js内容

 var day = document.getElementById('day')
        var hour = document.getElementById('hour')
        var minute = document.getElementById('minute')
        var second = document.getElementById('second')



        setInterval(function() {
            var now = new Date()
            var gettime = new Date('2021-6-7 0:0:0')
            var timeout = gettime - now
            day.innerText = Math.floor(timeout / (1000 * 60 * 60 * 24))
            hour.innerText = Math.floor(timeout / 1000 / 60 / 60 % 24)
            minute.innerText = Math.floor(timeout / 1000 / 60 % 60)
            second.innerText = Math.floor(timeout / 1000 % 60)
        }, 1000)

首先获取html里面的内容,然后利用Date的函数来计算出现在和输入时间的差值,然后通过运算符来计算出精确的天,时,分,秒
在这里插入图片描述
效果如上

完整代码

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

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .time-item {
            width: 450px;
            height: 45px;
            margin: 0 auto;
        }
        
        .time-item strong {
            background: orange;
            color: #fff;
            line-height: 49px;
            font-size: 36px;
            font-family: Arial;
            padding: 0 10px;
            margin-right: 10px;
            border-radius: 5px;
            box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.2);
        }
        
        .time-item>span {
            float: left;
            line-height: 49px;
            color: orange;
            font-size: 32px;
            margin: 0 10px;
            font-family: Arial, Helvetica, sans-serif;
        }
        
        .title {
            width: 330px;
            height: 50px;
            margin: 200px auto 50px auto;
        }
    </style>

</head>

<body>
    <h1 class="title">距离2021高考,还有</h1>

    <div class="time-item">
        <span><span id="day">00</span></span>
        <strong><span id="hour">00</span></strong>
        <strong><span id="minute">00</span></strong>
        <strong><span id="second">00</span></strong>
    </div>
    <script>
        var day = document.getElementById('day')
        var hour = document.getElementById('hour')
        var minute = document.getElementById('minute')
        var second = document.getElementById('second')



        setInterval(function() {
            var now = new Date()
            var gettime = new Date('2021-6-7 0:0:0')
            var timeout = gettime - now
            day.innerText = Math.floor(timeout / (1000 * 60 * 60 * 24))
            hour.innerText = Math.floor(timeout / 1000 / 60 / 60 % 24)
            minute.innerText = Math.floor(timeout / 1000 / 60 % 60)
            second.innerText = Math.floor(timeout / 1000 % 60)
        }, 1000)
    </script>
</body>

</html>

今天的代码就是这样啦,咋们下期见。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值