我用js做了一個向上計數的計數器動畫,但是當我刷新頁面時計數器就開始向上計數動畫(不管我在哪里,也不管我是否能看到它)。我希望計數器僅在計數器 div 位于視點中時(當我向下滾動并看到它時)開始工作。超文本標記語言<div class="content bg-warning"> <div class="row m-0 py-5 d-flex justify-content-center"> <div class="counter" data-count="2200" style="color: white; font-size: 32px; font-weight: bold;">0</div> </div></div>js<script type="text/javascript"> $('.counter').each(function() { var $this = $(this), countTo = $this.attr('data-count'); $({ countNum: $this.text()}).animate({ countNum: countTo }, { duration: 8000, easing:'linear', step: function() { $this.text(Math.floor(this.countNum)); }, complete: function() { $this.text(this.countNum); //alert('finished'); } }); });</script>我該如何解決這個問題?
滾動超出文檔中的特定點后激活動畫
胡子哥哥
2023-11-02 22:20:40