Skip to content

Instantly share code, notes, and snippets.

@0000marcell
Created October 27, 2019 16:45
Show Gist options
  • Save 0000marcell/9cdc15d7b38a90b271e310e64c4c1442 to your computer and use it in GitHub Desktop.
Save 0000marcell/9cdc15d7b38a90b271e310e64c4c1442 to your computer and use it in GitHub Desktop.
any part of the element is in the viewport
function isAnyPartOfElementInViewport(el) {
const rect = el.getBoundingClientRect();
const windowHeight = (window.innerHeight || document.documentElement.clientHeight);
const windowWidth = (window.innerWidth || document.documentElement.clientWidth);
const vertInView = (rect.top <= windowHeight) && ((rect.top + rect.height) >= 0);
const horInView = (rect.left <= windowWidth) && ((rect.left + rect.width) >= 0);
return (vertInView && horInView);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment