Skip to content

Instantly share code, notes, and snippets.

@jwelshiv
Created February 18, 2016 05:59
Show Gist options
  • Save jwelshiv/19cdb5b5921344f5d9ef to your computer and use it in GitHub Desktop.
Save jwelshiv/19cdb5b5921344f5d9ef to your computer and use it in GitHub Desktop.
function isElementVisible (el, withinEl) {
var withinRect = withinEl ?
withinEl.getBoundingClientRect() :
{ top: 0, left: 0, right: window.innerWidth, bottom: window.innerHeight };
var rect = el.getBoundingClientRect();
return (
rect.top >= withinRect.top &&
rect.left >= withinRect.left &&
rect.bottom <= withinRect.bottom &&
rect.right <= withinRect.right
);
}
@jwelshiv
Copy link
Author

Determine if one element is visible within the other

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