Skip to content

Instantly share code, notes, and snippets.

@mpjura
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpjura/e39fa78bfce18a916d5b to your computer and use it in GitHub Desktop.
Save mpjura/e39fa78bfce18a916d5b to your computer and use it in GitHub Desktop.
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