Created
November 7, 2020 07:22
-
-
Save SimonAKing/70b65862de61d7cf20ab10408c2675ce to your computer and use it in GitHub Desktop.
isElementInViewport
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
function isElementInViewport(el) { | |
http://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap | |
{ /* The source of the following code. */ } | |
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) | |
const isElementPartiallyInViewport = vertInView && horInView | |
if (isElementPartiallyInViewport) { return true } | |
return ( | |
(rect.left >= 0) && | |
(rect.top >= 0) && | |
((rect.left + rect.width) <= windowWidth) && | |
((rect.top + rect.height) <= windowHeight) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment