Skip to content

Instantly share code, notes, and snippets.

@fabiomsnunes
Created January 16, 2022 14:36
Show Gist options
  • Save fabiomsnunes/0c868cd2af6f4288462a204cc409825f to your computer and use it in GitHub Desktop.
Save fabiomsnunes/0c868cd2af6f4288462a204cc409825f to your computer and use it in GitHub Desktop.
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