Skip to content

Instantly share code, notes, and snippets.

@kaleem-elahi
Last active October 10, 2019 12:30
Show Gist options
  • Save kaleem-elahi/5a866a5848aaed682150a55ad035ad88 to your computer and use it in GitHub Desktop.
Save kaleem-elahi/5a866a5848aaed682150a55ad035ad88 to your computer and use it in GitHub Desktop.
Check if Element is visible in Viewport
export const isElementInViewport = (el) => {
var rect = el.getBoundingClientRect();
return rect.bottom > 0 &&
rect.right > 0 &&
rect.left < (window.innerWidth || document.documentElement.clientWidth) /* or $(window).width() */ &&
rect.top < (window.innerHeight || document.documentElement.clientHeight) /* or $(window).height() */;
}
isElementInViewport(<div />) // will return True of False based on Visibility in viewport
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment