Skip to content

Instantly share code, notes, and snippets.

@roman-manchenko
Created November 14, 2013 12:17
Show Gist options
  • Save roman-manchenko/7465866 to your computer and use it in GitHub Desktop.
Save roman-manchenko/7465866 to your computer and use it in GitHub Desktop.
(function () {
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
return define('jquery.in-viewport', ['jquery'], function ($) {
return factory($, root);
});
} else {
return factory(root.jQuery, root);
}
})(this, function($, window) {
var $win = $(window);
$.extend($.expr[':'], {
"in-viewport": function (element) {
var viewport = { top: $win.scrollTop()
, left : $win.scrollLeft()
},
$element = $(element),
bounds = $element.offset();
viewport.right = viewport.left + $win.width();
viewport.bottom = viewport.top + $win.height();
bounds.right = bounds.left + $element.outerWidth();
bounds.bottom = bounds.top + $element.outerHeight();
return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));
}
});
});
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment