Skip to content

Instantly share code, notes, and snippets.

Created January 13, 2015 19:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/f09b5cd487b5ad55c46e to your computer and use it in GitHub Desktop.
Save anonymous/f09b5cd487b5ad55c46e to your computer and use it in GitHub Desktop.
Enhanced isScrolledIntoView
$.fn.isInViewport = function(containerSelector, partial) {
var $element = this;
if($element.length) {
var el = $element[0];
var rect = el.getBoundingClientRect();
if(containerSelector && $(containerSelector).length) {
var parentRect = $(containerSelector)[0].getBoundingClientRect();
} else {
var parentRect = {top: 0, left: 0, bottom: $(window).height(), right: $(window).width()};
}
if(partial) {
return (
rect.left >= parentRect.left &&
rect.right <= parentRect.right &&
(
(rect.top >= parentRect.top && rect.top <= parentRect.bottom) ||
(rect.bottom >= parentRect.top && rect.bottom <= parentRect.bottom)
)
);
} else {
return (
rect.top >= parentRect.top &&
rect.left >= parentRect.left &&
rect.bottom <= parentRect.bottom &&
rect.right <= parentRect.right
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment