Skip to content

Instantly share code, notes, and snippets.

@colxi
Created August 2, 2018 02:54
Show Gist options
  • Save colxi/6f7b30d470771f7dedb8005330044157 to your computer and use it in GitHub Desktop.
Save colxi/6f7b30d470771f7dedb8005330044157 to your computer and use it in GitHub Desktop.
detect if HTML element is displayed in browser viewport
function isElementInViewport (el) {
var rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment