📅  最后修改于: 2022-03-11 15:03:38.533000             🧑  作者: Mango
// 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)
);
}
// The above function could be used by adding a “scroll” event listener to the window and then calling isInViewport().