Guys ,want my countup to work when it appears on viewport. How to build the code right. Cuz this code works fine, but with an issue: it stops to count when I scroll and when stop – continues to count. Where to search ?
$(document).ready(function() {
$(window).scroll( function(){
$('.counter').each(function() {
var $this = $(this), countTo = $this.attr('data-count');
var bottom_of_object = $(this).position().top + $(this).outerHeight();
var bottom_of_window = $(window).scrollTop() + $(window).height();
if( bottom_of_window > bottom_of_object ){
$({ 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');
}
});
}
});
});
});