Tween.js动画函数使用demo

案例:

  • 小球的运动
  • 数字的自增
    在这里插入图片描述

demo

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tween.js/20.0.0/tween.umd.js"></script>
    <style>
      .circle {
        width: 100px;
        height: 100px;
        border-radius: 50%;
        background-color: orange;
        position: absolute;
      }
      #box {
        background-color: deeppink;
        width: 100px;
        height: 100px;
      }
    </style>
  </head>
  <body>
    <div class="circle" style="top: 0px; left: 0px"></div>
    <div id="box"></div>
    <script>
      const position = { x: 0 }; // position 对应初始位置 也就是left 是0px
      const div = document.querySelector(".circle"); // 获取dom元素
      const tweenA = new TWEEN.Tween(position) // new TWEEN.Tween里面放置初始位置,也就是left 是0px
        .to({ x: 500 }, 1000) // to函数第一个参数里面放置要到哪里 也就是left:500px的地方
        // to函数第二个参数是要花多少时间,也就是1000毫秒
        .onUpdate(function () {
          // onUpdate函数放置这段时间我们做什么,也就是div的left值不断变化,
          div.style.left = position.x + "px"; // position.x就是上面说的不断变化的left值,
        });
      tweenA.start(); // 启动这个动画
      tweenA.yoyo(true)
      tweenA.repeat(3)

      const box = document.getElementById("box"); // Get the element we want to animate.
      const coords = { x: 0, y: 0 }; // Start at (0, 0)
      const tweenB = new TWEEN.Tween(coords, false) // Create a new tween that modifies 'coords'.
        .to({ x: 300, y: 200 }, 1000) // Move to (300, 200) in 1 second.
        // .easing(TWEEN.Easing.Quadratic.InOut) // Use an easing function to make the animation smooth.
        .onUpdate((object) => {
          // console.log(object.x)
          // Called after tween.js updates 'coords'.
          // Move 'box' to the position described by 'coords' with a CSS translation.
          box.style.setProperty(
            "transform",
            "translate(" + coords.x + "px, " + coords.y + "px)"
          );
        })
        .start(); // Start the tween immediately.

      let data = { num: 100 };
      const tweenC = new TWEEN.Tween(data)
        .to({ num: 230 }, 1000).easing(TWEEN.Easing.Quadratic.InOut)
        .onUpdate((a) => {
          num = Math.floor(data.num);
          console.log(num);
          box.innerText = num
        })
        .start()

      function animate() {
        requestAnimationFrame(animate); // requestAnimationFrame可以看成setTimeout(animate, 17)
        tweenA.update(); // 每隔一段时间,update方法会调用上面的onUpdate函数,这样让left变化,小球位置也变化
        tweenB.update();
        tweenC.update();
        // tweenA.chain(tweenB)
        // tweenB.chain(tweenA)
      }
      animate();
    </script>
  </body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

C+ 安口木

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

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

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

打赏作者

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

抵扣说明:

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

余额充值