Dom(文档对象模型),间歇函数钟表小案例

119 阅读1分钟

目的:让钟表的秒钟转起来:

1.用动画做出来,先设置动画让秒钟延z轴旋转360°,然后调用动画让动画分60次转完

<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>
    *{
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    .box {
      /* perspective: 800px; */
      position: relative;
      width: 600px;
      height: 600px;
      margin: 50px auto;
      background: url(./images/clock.jpg) no-repeat center;
    }

    .box .hh,
    .mm,
    .ss{
      width: 100%;
      height: 100%;
    }

    .box .hh{
      position: absolute;
      background: url(./images/hour.png) no-repeat center ;
    }

    .box .mm{
      position: absolute;
      background: url(./images/minute.png) no-repeat center;
      transform: rotateZ(-90deg);
    }

    .box .ss {
      position: absolute;
      background:  url(./images/second.png) no-repeat center;
      animation: move 60s infinite steps(60);
      /* transform: rotateZ(-90deg); */
    }


    @keyframes move {
     100% {
        transform: rotateZ(360deg);
      }
    }
  </style>
</head>
<body>
  
  <div class="box">
    <div class="hh">
     
    </div>
    <div class="mm">

    </div>
    <div class="ss">

    </div>
  </div>
</body>
</html>

2.用间歇函数做出来,旋转360°,设定一秒钟转一次,一次转6°

<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>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    .box {
      /* perspective: 800px; */
      position: relative;
      width: 600px;
      height: 600px;
      margin: 50px auto;
      background: url(./images/clock.jpg) no-repeat center;
    }

    .box .hh,
    .mm,
    .ss {
      width: 100%;
      height: 100%;
    }

    .box .hh {
      position: absolute;
      background: url(./images/hour.png) no-repeat center;
    }

    .box .mm {
      position: absolute;
      background: url(./images/minute.png) no-repeat center;
      transform: rotateZ(-90deg);
    }

    .box .ss {
      position: absolute;
      background: url(./images/second.png) no-repeat center;
    }
  </style>
</head>

<body>

  <div class="box">
    <div class="hh">

    </div>
    <div class="mm">

    </div>
    <div class="ss">

    </div>
  </div>

    <script>

      const ss = document.querySelector('.ss')
      let move = 0

      setInterval(function () {
        move += 6
        
        if (move >= 360){
          move = 0
        }
        ss.style.transform = `rotate(${move}deg)`
      }, 1000)
    </script>
  
</body>

</html>