How to run a js script on a certain place?

Hi all.
There is a script of an animation counter, and this script is loaded immediately when you run the page. And when you get to a certain point, the counter has already finished its job.
You must make sure that the script started to work when the screen came to these elements or blocks.

$('.count').each(function () {
$(this).prop('Counter',0).animate({
 Counter: $(this).text()
 }, {
 duration: 5555,
 easing: 'swing',
 step: function (now) {
$(this).text(Math.ceil(now));
}
});
});
June 7th 19 at 14:54
1 answer
June 7th 19 at 14:56
Put the code into a function and call it on the event scroll when it reaches the desired element:

function start_count() {
 $('.count').each(function () {
$(this).prop('Counter',0).animate({
 Counter: $(this).text()
 }, {
 duration: 5555,
 easing: 'swing',
 step: function (now) {
$(this).text(Math.ceil(now));
}
});
});
}

$(function() {
 var oTop = $('.count').offset().top - window.innerHeight;
$(window).scroll(function(){
 var pTop = $('body').scrollTop();
 if( pTop > oTop ){
start_count();
}
});
});
counter actually stalled) - noel.Schmeler83 commented on June 7th 19 at 14:59

Find more questions by tags JavaScript