Last active
October 10, 2019 12:30
-
-
Save kaleem-elahi/5a866a5848aaed682150a55ad035ad88 to your computer and use it in GitHub Desktop.
Check if Element is visible in Viewport
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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