I want to count up a number when the content is in the visible area of the viewport. At the moment, I've an animation but it starts on page load. So the user can't see it when he arrives at the content (bottom of the page).
Here's my JS code to count up the numbers:
$('.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');
}
});
});
It is from this example: https://codepen.io/hi-im-si/pen/uhxFn
Is there any way to start the animation when the numbers hit the visible area of the page?