js倒计时效果

<!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>
    .countdown {
      width: 240px;
      height: 305px;
      text-align: center;
      line-height: 1;
      color: #fff;
      background-color: brown;
      /* background-size: 240px; */
      /* float: left; */
      overflow: hidden;
    }

    .countdown .next {
      font-size: 16px;
      margin: 25px 0 14px;
    }

    .countdown .title {
      font-size: 33px;
    }

    .countdown .tips {
      margin-top: 40px;
      margin-bottom: 30px;
      font-size: 23px;
    }

    .countdown small {
      font-size: 17px;
    }

    .countdown .clock {
      width: 142px;
      margin: 18px auto 0;
      overflow: hidden;
    }

    .countdown .clock span,
    .countdown .clock i {
      display: block;
      text-align: center;
      line-height: 34px;
      font-size: 23px;
      float: left;
    }

    .countdown .clock span {
      width: 34px;
      height: 34px;
      border-radius: 2px;
      background-color: #303430;
    }

    .countdown .clock i {
      width: 20px;
      font-style: normal;
    }
  </style>
</head>

<body>
  <div class="countdown">
    <p class="next">今天是年2月22日</p>
    <p class="title">开学倒计时</p>
    <p class="tips">开学倒计时</p>
    <p class="clock">
      <span id="hour">00</span>
      <i>:</i>
      <span id="minutes">25</span>
      <i>:</i>
      <span id="scond">20</span>
    </p>
  </div>
  <script>
    function getNowDay(){
      const date = new Date()
      year = date.getFullYear()
      month = date.getMonth()+1
      ri = date.getDate()
      const next = document.querySelector('.next')
      next.innerHTML = `今年是${year}年${month}月${ri}日`
    }
    function getCountTime(){
      //当前时间戳
      const now = +new Date()
      //未来时间戳
      const last = +new Date('2024-3-1 18:00:00')
      //得到剩余时间戳
      const count = (last-now)/1000
      //h = parseInt(总秒数/60/60 % 24)
      //m = parseInt(总秒数/60%60)
      //s = parseInt(总秒数%60)
      let d = parseInt(count/60/60/24)
      let h = parseInt(count/60/60%24)
      h = h<10 ? '0'+h : h
      let m = parseInt(count/60%60)
      m = m<10 ? '0'+m : m
      let s = parseInt(count%60)
      s = s<10 ? '0'+s : s
      const hour = document.querySelector('#hour')
      const minutes = document.querySelector('#minutes')
      const scond = document.querySelector('#scond')
      hour.innerHTML = h
      minutes.innerHTML = m
      scond.innerHTML = s
      const tips = document.querySelector('.tips')
      tips.innerHTML =`${d}天`
    }
    getCountTime()
    getNowDay()
    setInterval(function (){
      getNowDay()
      getCountTime()
    },1000)
  </script>
</body>

</html>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林代码er

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值