Skip to content

Instantly share code, notes, and snippets.

@radius
Created August 20, 2013 18:48
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 radius/6285570 to your computer and use it in GitHub Desktop.
Save radius/6285570 to your computer and use it in GitHub Desktop.
jquery function to tell if a dom element is within the viewport
$.fn.isOnScreen = function(){
var win = $(window);
var viewport = {
top : win.scrollTop(),
left : win.scrollLeft()
};
viewport.right = viewport.left + win.width();
viewport.bottom = viewport.top + win.height();
var bounds = this.offset();
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));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment