実現したいこと
0からカウントスタートさせて、目標数まで3秒でカウントアップを完了させたいです。
発生している問題・エラーメッセージ
3秒でカウントアップアニメーションが完了しない。
どなたかご助言していただけると助かります、、!
よろしくお願いいたします。
該当のソースコード
html
1<p class="count_description">総売上<span class="count" id="count02" data-num="6000">0</span>万</p>
JavaScript
1var success = true; 2 $(window).scroll(function() { 3 if(success){ 4 var elm = $('.count'); 5 6 var elmTop = elm.offset().top; 7 var scr_count = $(window).scrollTop() + (window.innerHeight/2); 8 9 if (scr_count > elmTop) { 10 success = false; 11 elm.each(function(){ 12 var self = $(this), 13 countMax = self.attr('data-num'), 14 thisCount = self.text(), 15 countTimer; 16 17 function timer(){ 18 var countSpeed = 3000/new Number(countMax - thisCount); 19 countTimer = setInterval(function(){ 20 var countNext = thisCount++; 21 self.text(countNext); 22 23 if(countNext == countMax){ 24 clearInterval(countTimer); 25 } 26 },countSpeed); 27 } 28 timer(); 29 }); 30 } 31 } 32 });
###解決法
https://github.com/bfintal/Counter-Up
上記のプラグインを導入したところ、下記のコードだけで動作しました。
JavaScript
1jQuery(document).ready(function($) { 2 $('.count').counterUp({ 3 delay: 25, 4 time: 1000 5 }); 6 });
回答2件
あなたの回答
tips
プレビュー