Skip to content

Instantly share code, notes, and snippets.

@mpjura
Last active Aug 29, 2015
Embed
What would you like to do?
Debounced Scroll for Ads
if ( !window.requestAnimationFrame ){
window.requestAnimationFrame = (function(){
return window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( callback ) {
window.setTimeout( callback, 1000 / 60 );
};
})();
}
var eventName = 'scroll.bottom-ads',
lastScrollY = 0,
ticking = false;
$( window ).on( eventName, function( evt ) {
lastScrollY = window.pageYOffset;
if ( !ticking ) {
requestAnimationFrame( handleScroll );
ticking = true;
}
});
function handleScroll() {
var bottomOfWindow = lastScrollY + $( window ).height();
var topOfDiv = $('#gpt_gallery_smarttout').offset().top;
if ( bottomOfWindow > topOfDiv && bottomAdsFired === 0 ) {
console.log('Load My Ads Now!');
bottomAdsFired = true;
$( window ).off( eventName );
}
ticking = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment