Skip to content

Instantly share code, notes, and snippets.

@Takazudo
Created May 12, 2011 07:30
Show Gist options
  • Save Takazudo/968093 to your computer and use it in GitHub Desktop.
Save Takazudo/968093 to your computer and use it in GitHub Desktop.
/*!
* $.fn.isInWindow
*
* version 0.0.1 (2010/11/17)
* author: Takeshi Takatsudo (takazudo[at]gmail.com)
* license: MIT
* depends: jQuery 1.4.4
*/
(function($, undefined){ // start encapsulation
var $win = $(window);
$.fn.isAboveTheWindow = function(opitons){
var $el = this;
if($el.size()>1){
$.error('2 or more elements were thrown.');
return false;
}
var o = $.extend({
threshold: 0
},opitons);
return ($win.scrollTop() >= $el.offset().top + o.threshold + $el.innerHeight());
};
$.fn.isBelowTheWindow = function(options){
var $el = this;
if($el.size()>1){
$.error('2 or more elements were thrown.');
return false;
}
var o = $.extend({
threshold: 0
},options);
return $win.height() + $win.scrollTop() <= $el.offset().top - o.threshold;
};
$.fn.isInWindow = function(){
var $el = this;
return !$el.isAboveTheWindow() && !$el.isBelowTheWindow();
};
})(jQuery); // end encapsulation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment