Skip to content

Instantly share code, notes, and snippets.

@verticalgrain
Created December 17, 2015 03:27
Show Gist options
  • Select an option

  • Save verticalgrain/4dd687f64e1d183f4cd9 to your computer and use it in GitHub Desktop.

Select an option

Save verticalgrain/4dd687f64e1d183f4cd9 to your computer and use it in GitHub Desktop.
Throttle scroll based interaction trigger
function throttle(callback, limit) {
var wait = false;
return function () {
if (!wait) {
callback.call();
wait = true;
setTimeout(function () {
wait = false;
}, limit);
}
};
},
function statisticsAnimation() {
if ( !$('.statistics').hasClass('is-animated')) {
var scrollPos = $(document).scrollTop(),
windowHeight = $(window).height(),
windowBottomPos = scrollPos + windowHeight - 100,
statsOffset = $('.statistics').offset(),
statsPos = statsOffset.top;
if (windowBottomPos > statsPos) {
console.log('it happened at: ' + windowBottomPos);
$('.statistics').addClass('is-animated');
}
}
},
$(window).load(function() {
statisticsAnimation();
window.addEventListener("scroll", throttle(statisticsAnimation, 200 ));
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment