复杂动画

61 阅读1分钟

效果

动画.gif

代码

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div class="box"></div>
    <input type="range" min="0" max="1" step="0.01" value="0" />
    <style>
      .box {
        --delay: 0;
        width: 50px;
        height: 50px;
        background-color: red;
        border-radius: 50%;
        animation: animate 1s var(--delay) linear forwards paused;
      }
      @keyframes animate {
        0% {
          transform: translate(0);
        }
        100% {
          transform: translate(200px);
        }
      }
    </style>
    <script>
      let box = document.querySelector(".box");
      let input = document.querySelector("input");
      input.oninput = () => {
        box.style.setProperty("--delay", `-${input.value}s`);
      };
    </script>
  </body>
</html>