jquery动态插件之gsap和TextPlugin

在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
  <title>数字化人才认证数动画</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.5/gsap.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.5/TextPlugin.min.js"></script> <style>
    .counter {
      font-size: 24px;
      font-weight: bold;
    }
  </style>
</head>
<body>
  <span class="counter" data-target="12345">0</span> 

  <script>
    $(document).ready(function() {
      gsap.registerPlugin(TextPlugin); // 注册 TextPlugin 插件

      $('.counter').each(function() {
        const target = parseInt($(this).data('target'));
        gsap.to($(this), {
          duration: 1, 
          ease: "power2.out", 
          text: target, 
          onUpdate: function() {
            $(this.target).text(Math.round($(this.target)._gsap.get(0)));
          }
        });
      });
    });
  </script>
</body>
</html>

多个数字使用一个方法会从上到下加载。

报错:
Uncaught TypeError:
Cannot read properties of undefined (reading ‘get’) at Tween.onUpdate [as _onUpdate] 。

onUpdate: function() {
  // 这里的	this.target 是undefined
  $(this.target).text(Math.round(this.target._gsap.get(0)));
}

解决方法:

onUpdate: function() {
  $(this.target).text(Math.round($(this.target)._gsap.get(0)));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值