1// Determine if an element is in the visible viewport
2function isInViewport(element) {
3 var rect = element.getBoundingClientRect();
4 var html = document.documentElement;
5 return (
6 rect.top >= 0 &&
7 rect.left >= 0 &&
8 rect.bottom <= (window.innerHeight || html.clientHeight) &&
9 rect.right <= (window.innerWidth || html.clientWidth)
10 );
11}
12// The above function could be used by adding a “scroll” event listener to the window and then calling isInViewport().