Skip to content

Instantly share code, notes, and snippets.

@Tomotoes
Created November 7, 2020 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tomotoes/70b65862de61d7cf20ab10408c2675ce to your computer and use it in GitHub Desktop.
Save Tomotoes/70b65862de61d7cf20ab10408c2675ce to your computer and use it in GitHub Desktop.
isElementInViewport
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