实现一个计时圆盘

198 阅读1分钟
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .container {
      width: 200px;
      height: 200px;
      position: relative;
    }

    .container .wrapper {
      position: absolute;
      width: 100px;
      height: 200px;
      top: 0;
      overflow: hidden;
    }

    .container .left {
      left: 0
    }

    .container .right {
      right: 0
    }

    .container .circle {
      position: absolute;
      height: 160px;
      width: 160px;
      border: 20px solid transparent;
      border-radius: 50%;
    }

    .container .leftcircle {
      left: 0;
      border-bottom: 20px solid green;
      border-left: 20px solid green;
      transform: rotate(45deg);
     /*没有js时添加动画 name duration function 循环 配合keyframe*/
      animation: circle_left 5s linear infinite;
    }

    .container .rightcircle {
      right: 0;
      border-top: 20px solid green;
      border-right: 20px solid green;
      transform: rotate(45deg);
      animation: circle_right 5s linear infinite;
    }
    
    /* 实现计时数字居中 */
    .container #show {
      position: absolute;
      width: 100px;
      height: 100px;
      top: 100px;
      left: 100px;
      transform: translate(-50%, -50%);
      text-align: center;
      line-height: 100px;
    }

    /* @keyframes circle_left {

      0%,
      50% {
        transform: rotate(-135deg);
      }

      100% {
        transform: rotate(45deg);
      }

    }

    @keyframes circle_right {
      0% {
        transform: rotate(-135deg);
      }

      50%,
      100% {
        transform: rotate(45deg);
      }
    } */
  </style>
</head>

<body>
  <div class="container">
    <div class="wrapper left">
      <div class="circle leftcircle" id="left"></div>
    </div>
    <div class="wrapper right">
      <div class="circle rightcircle" id="right"></div>
    </div>
    <div id="show">50</div>
  </div>

  <script>
    function getTime() {
      var right = document.getElementById("right");
      var left = document.getElementById("left");
      var show = document.getElementById("show");
      var date = new Date()
      var second = date.getSeconds()
      show.innerHTML = second;
      if (second <= 30) {
        right.style.cssText = "transform:rotate(" + (-135 + 6 * second) + "deg)"
        left.style.cssText = "transform:rotate(-135deg)"
      } else {
        right.style.cssText = "transform:rotate(45deg)"
        left.style.cssText = "transform:rotate(" + (-135 + 6 * (second - 30)) + "deg)"

      }
    }
    getTime()
    setInterval(() => {
      getTime()
    }, 1000)
    
  </script>

</body>

</html>