Skip to content

Instantly share code, notes, and snippets.

@lili21
Last active August 29, 2015 14:21
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 lili21/4c4edd06fd3f23d8c71e to your computer and use it in GitHub Desktop.
Save lili21/4c4edd06fd3f23d8c71e to your computer and use it in GitHub Desktop.
determine if an element is in the visible viewport, 判断元素是否在可视区域内
// Determine if an element is in the visible viewport
function isInViewport(element) {
var rect = element.getBoundingClientRect();
var html = document.documentElement;
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || html.clientHeight) &&
rect.right <= (window.innerWidth || html.clientWidth)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment