如何用html+css+js写你的倒计时

1.运行效果如下:

在这里插入图片描述

2.涉及到知识点有:
Date对象方法、Math对象方法
ES6知识包括:块作用域、解构赋值、模板字符串、箭头函数

3.代码如下(可以自行新建一个html文件粘贴代码后运行试试看):

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>倒计时</title>
  <style>
    *{margin:0;padding:0;}
    div{
      font-size:30px;
    }
    span{
      color:purple;
    }
  </style>
</head>
<body>
  <div>距离2021年的六一儿童节还有:<span id="time1"></span></div>
  <div>距离2021年的六一儿童节还有:<span id="time2"></span></div>
  <div>距离2021年的六一儿童节还有:<span id="time3"></span></div>
  <div>距离2021年的六一儿童节还有:<span id="time4"></span></div>

  <script>
    const time1 = document.getElementById("time1")
    const time2 = document.getElementById("time2")
    const time3 = document.getElementById("time3")
    const time4 = document.getElementById("time4")
    const {floor} = Math

    let addZero = n => n < 10 ? "0" + n : n + "";

    // 
    const aimDate = new Date("2021/6/1") //确定目标时间
    
    const fn = () => {
      const date = aimDate - new Date()
      
      // 天数
      const D = floor(date/24/60/60/1000)
      // 小时
      const h = floor(date/60/60/1000)%24
      // 分钟
      const m = floor(date/60/1000)%60
      // 秒钟
      const s = floor(date/1000)%60
      
      time1.innerText = `${D}天`
      time2.innerText = `${D}天${addZero(h)}小时`
      time3.innerText = `${D}天${addZero(h)}小时${addZero(m)}分钟`
      time4.innerText = `${D}天${addZero(h)}小时${addZero(m)}分钟${addZero(s)}秒`
    }

    setInterval(fn, 1000)
    
  </script>
</body>
</html>

4.这里可以自行拓展成其他的倒计时,比如:
(1)下一次双十一时间
(2)下一次春节
(3)下一次母亲节

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值