Created
January 16, 2022 14:36
-
-
Save fabiomsnunes/0c868cd2af6f4288462a204cc409825f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(function($) { | |
$( document ).ready(function() { | |
if ($('.animatedCounter').length) { | |
$(window).scroll(testScroll); | |
} | |
}); | |
var viewed = false; | |
function isScrolledIntoView(elem) { | |
var docViewTop = $(window).scrollTop(); | |
var docViewBottom = docViewTop + $(window).height(); | |
var elemTop = $(elem).offset().top; | |
var elemBottom = elemTop + $(elem).height(); | |
return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop)); | |
} | |
function testScroll() { | |
if (isScrolledIntoView($(".animatedCounter")) && !viewed) { | |
viewed = true; | |
$('.animatedCounter').each(function () { | |
$(this).prop('Counter',0).animate({ | |
Counter: $(this).text() | |
}, { | |
duration: 1000, | |
easing: 'swing', | |
step: function (now) { | |
$(this).text(Math.ceil(now)); | |
} | |
}); | |
}); | |
} | |
} | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment