gsap 原生html 数字递增滚动

265 阅读1分钟

#`

Document
<script src="./gsap.js"></script> 引入gsap的地址 

数字渐变

0
<script>
  let end = 100;
  let start = 0;
  let numberElement = document.querySelector('.box2');
 
  gsap.to(numberElement, {
    duration: 5, // 动画持续时间(秒)
    end: 100, // 终值,即数字滚动到的最终值
    roundProps: 'end', // 将最终值四舍五入到整数
    onUpdate: function () {
      if (start > 100) return; 如果超出结束
      // 在动画更新时更新数字元素的文本内容
      numberElement.textContent = start++;

      console.log(this);
    },
  });
</script>
`