Skip to content

Instantly share code, notes, and snippets.

@claviska
Created October 30, 2013 21:36
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save claviska/7240757 to your computer and use it in GitHub Desktop.
Save claviska/7240757 to your computer and use it in GitHub Desktop.
jQuery offscreen plugin
/*
* jQuery offscreen plugin
*
* Filters that detect when an element is partially or completely outside
* of the viewport.
*
* Usage:
*
* $('#element').is(':off-bottom')
*
* The above example returns true if #element's bottom edge is even 1px past
* the bottom part of the viewport.
*
* Copyright Cory LaViska for A Beautiful Site, LLC. (http://www.abeautifulsite.net/)
*
* Licensed under the MIT license: http://opensource.org/licenses/MIT
*
*/
(function($) {
$.extend($.expr[':'], {
'off-top': function(el) {
return $(el).offset().top < $(window).scrollTop();
},
'off-right': function(el) {
return $(el).offset().left + $(el).outerWidth() - $(window).scrollLeft() > $(window).width();
},
'off-bottom': function(el) {
return $(el).offset().top + $(el).outerHeight() - $(window).scrollTop() > $(window).height();
},
'off-left': function(el) {
return $(el).offset().left < $(window).scrollLeft();
}
});
})(jQuery);
@nboliver
Copy link

This is handy!

@claviska
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment