翻页时钟或者日历

效果:

<!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>
    *{
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }
    #app {
      height: 100%;
      font-size: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-flow: wrap-reverse;
    }

    .box {
      position: relative;
      box-sizing: border-box;
      margin: 5px;
    }

    .box .child {
      width: 130px;
      height: 100px;
      overflow: hidden;
    }

    .box>.top {
      background-color: #333;
      line-height: 200px;
      border-bottom: 2px solid #fff;
      border-radius: 4px 4px 0 0;
    }

    .box .flip {
      position: absolute;
      top: 0px;
      z-index: 1;
      transform-origin: bottom;
      border-radius: 4px 4px 0 0;
    }

    .box .flip .flip_face {
      position: absolute;
      background-color: #333;
      line-height: 200px;
      z-index: 1;
      border-bottom: 2px solid #fff;
    }

    .box .flip .flip_back {
      position: absolute;
      background-color: #333;
      line-height: 0px;
      transform: perspective(500px) rotateX(0deg) rotateY(-180deg) rotate(180deg);
      border-top: 2px solid #fff;
    }

    .box .bottom {
      background-color: #333;
      line-height: 0px;
      border-top: 2px solid #fff;
      border-radius: 0 0 4px 4px;
    }

    .text {
      text-align: center;
      font-size: 100px;
      font-weight: 900;
      color: #fff;
    }
  </style>
</head>

<body>
  <div id="app">
    <div class="box" id="box_minute">
      <div class="top child text" id="top">0</div>
      <div class="flip child" id="flip">
        <div class="flip_face child text" id="flip_face">0</div>
        <div class="flip_back child text" id="flip_back">0</div>
      </div>
      <div class="bottom child text" id="bottom">0</div>
    </div>
  </div>

  <script>
    // 获取Dom
    const box_minute = document.querySelector("#box_minute");
    const Top = box_minute.querySelector("#top");
    const flip = box_minute.querySelector("#flip");
    const flipFace = flip.querySelector("#flip_face");
    const flipBack = flip.querySelector("#flip_back");
    const Bottom = box_minute.querySelector("#bottom");
    let timer = null, timerTwo = null;

    const OneCycle = (n, timer) => { // 一次翻页的周期
      let num = 0;
      flipFace.style.zIndex = 1;
      flipBack.style.zIndex = 0;
      flip.style.transform = "perspective(500px) rotateX(0deg)"; // rotateX(0deg) => rotateX(-180deg)
      flipFace.innerHTML = n;
      Bottom.innerHTML = n;

      timer = setInterval(() => {
        num++;
        if (num > 50) {
          clearInterval(timer);
          return;
        };

        if (num === 1) {
          Top.innerHTML = (n + 1) === 60 ? 0 : (n + 1); // 60 和 0 在时间里,其实是一样的
          flipBack.innerHTML = (n + 1) === 60 ? 0 : (n + 1);
        }

        if (num <= 25) {
          flipFace.style.zIndex = 1;
          flipBack.style.zIndex = 0;
        } else {
          flipFace.style.zIndex = 0;
          flipBack.style.zIndex = 1;
        }
        // console.log(num); // 1 ~ 50
        flip.style.transform = `perspective(500px) rotateX(-${180 * num / 50}deg)`
      }, 20); // 将一秒钟分成50份。
    };

    const cycle = () => {
      timerTwo = setInterval(() => {
        let num = new Date().getSeconds();
        OneCycle(num, timer);
      }, 1000);
    };

    cycle();

    // 浏览器页面切换时,定时器setInterval抖动问题
    document.onvisibilitychange = function () {
      if (document.visibilityState === "visible") {
        cycle();
      } else {
        clearInterval(timer);
        clearInterval(timerTwo);
      }
    }

  </script>
</body>

</html>

浏览器页面切换时,定时器 setInterval 抖动问题?
利用 document.onvisibilitychange 来监听页面(可视区)是否变化,只要发生改变就会触发一次,再通过 document.visibilityState 或者 document.hidden 来判断用户是是否在该页面

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值