I am trying to trigger this code each time someone scrolls to that section, the code works fine because it does trigger but it only does it once. I am trying to think of the easiest way to make it trigger every time someone scrolls to that section. I tried taking off the !viewed from the condition and it does trigger but the countdown then goes backwards, not really sure why.
This is the code I have but only triggers once.
<script>
var viewed = false;
function isScrolledIntoView(elem) {
var docViewTop = jQuery(window).scrollTop();
var docViewBottom = docViewTop + jQuery(window).height();
var elemTop = jQuery(elem).offset().top;
var elemBottom = elemTop + jQuery(elem).height();
return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}
function testScroll() {
if (isScrolledIntoView(jQuery(".numbers")) && !viewed) {
jQuery('.value').each(function () {
jQuery(this).prop('Counter',0).animate({
Counter: jQuery(this).text()
}, {
duration: 4000,
easing: 'swing',
step: function (now) {
jQuery(this).text(Math.ceil(now));
}
});
});
}
}
</script>
Here is the link to my current code.