Skip to content

Instantly share code, notes, and snippets.

@ingowennemaring
Created August 6, 2015 15:59
Show Gist options
  • Save ingowennemaring/8ffd56b48a6ea58f5ad5 to your computer and use it in GitHub Desktop.
Save ingowennemaring/8ffd56b48a6ea58f5ad5 to your computer and use it in GitHub Desktop.
Check if an element is in the viewport
( function ( $ ) {
$.fn.isOnScreen = function () {
var win = $( window ),
bounds = this.offset(),
viewport = {
top: win.scrollTop(),
left: win.scrollLeft()
};
viewport.right = viewport.left + win.width();
viewport.bottom = viewport.top + win.height();
bounds.right = bounds.left + this.outerWidth();
bounds.bottom = bounds.top + this.outerHeight();
return ( !( viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top ||
viewport.top > bounds.bottom ) );
};
} )( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment