Skip to content

Instantly share code, notes, and snippets.

@TimoM
Created June 6, 2019 11:10
Show Gist options
  • Select an option

  • Save TimoM/c504d3b2cc95161371d449afeca36334 to your computer and use it in GitHub Desktop.

Select an option

Save TimoM/c504d3b2cc95161371d449afeca36334 to your computer and use it in GitHub Desktop.
Check if element is in viewport
function isVisibleInViewport(elem)
{
var y = elem.offsetTop;
var height = elem.offsetHeight;
while ( elem = elem.offsetParent )
y += elem.offsetTop;
var maxHeight = y + height;
var isVisible = ( y < ( window.pageYOffset + window.innerHeight ) ) && ( maxHeight >= window.pageYOffset );
return isVisible;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment