Skip to content

Instantly share code, notes, and snippets.

@fd
Created September 30, 2010 14:55
Show Gist options
  • Save fd/604705 to your computer and use it in GitHub Desktop.
Save fd/604705 to your computer and use it in GitHub Desktop.
jQuery plugin for getting elements above, in or below the viewport
(function($){
$.fn.viewportState = (function(){
var results = [];
this.each(function(){
var bounds = this.getBoundingClientRect();
if (window.viewport.height() < bounds.top) {
results.push(['below', $(this)]);
} else if (bounds.bottom <= 0) {
results.push(['above', $(this)]);
} else {
results.push(['screen', $(this)]);
};
});
return results;
});
$.fn.inViewport = (function(){
var results = [];
this.each(function(){
var bounds = this.getBoundingClientRect();
if (window.viewport.height() < bounds.top) {
// ignore
} else if (bounds.bottom <= 0) {
// ignore
} else {
results.push(this);
};
});
return $(results);
});
$.fn.aboveViewport = (function(){
var results = [];
this.each(function(){
var bounds = this.getBoundingClientRect();
if (window.viewport.height() < bounds.top) {
// ignore
} else if (bounds.bottom <= 0) {
results.push(this);
} else {
// ignore
};
});
return $(results);
});
$.fn.belowViewport = (function(){
var results = [];
this.each(function(){
var bounds = this.getBoundingClientRect();
if (window.viewport.height() < bounds.top) {
results.push(this);
} else if (bounds.bottom <= 0) {
// ignore
} else {
// ignore
};
});
return $(results);
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment