📌  相关文章
📜  如何检查元素是否在视口中 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:10.598000             🧑  作者: Mango

代码示例2
function isInViewport(element) {
    const rect = element.getBoundingClientRect();
    return (
        rect.top >= 0 &&
        rect.left >= 0 &&
        rect.bottom <= ((window.innerHeight + rect.height) || document.documentElement.clientHeight) &&
        rect.right <= (window.innerWidth || document.documentElement.clientWidth)
    );
}

//optimized from the stackOverflow answer to account 
//for element heights and widths (in vertical/horizontal scrolling)