Last active
August 29, 2015 14:06
-
-
Save mpjura/e39fa78bfce18a916d5b to your computer and use it in GitHub Desktop.
Debounced Scroll for Ads
This file contains hidden or 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
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