Skip to content

Instantly share code, notes, and snippets.

@frozonfreak
Created November 17, 2016 04:11
Show Gist options
  • Save frozonfreak/454fa6b41596001ee2a74d879d5e50aa to your computer and use it in GitHub Desktop.
Save frozonfreak/454fa6b41596001ee2a74d879d5e50aa to your computer and use it in GitHub Desktop.
check if element in view with jquery - for infinite scroll
$.fn.inView = function(){
if(!this.length) return false;
var rect = this.get(0).getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
};
$(window).on('scroll',function(){
if( $(<element>).inView() ) {
console.log('Visible');
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment