Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sarfarazansari/64c18e3a8f065cd33e5713c783ec6009 to your computer and use it in GitHub Desktop.
Save sarfarazansari/64c18e3a8f065cd33e5713c783ec6009 to your computer and use it in GitHub Desktop.
Check for html element is visible in viewport by pure javascript (vanilla)
function isVisibleInViewport(id) {
var element = document.getElementById(id);
var rect = element.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
//init
console.log(isInViewport("my-element"));
//return true or false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment