当超过0%的元素在视口/窗口中时返回true

Return true when more than 0% of element is in viewport / window

本文关键字:窗口 true 返回 视口 元素      更新时间:2023-09-26

因此,当作为参数给定的元素完全在视口/窗口中时,下面的代码返回true。

当元素的任何一位或任何超过0%的元素在视口中时,我如何更改它,使其返回true?

function isElementInViewport(el){
        var rect = el.getBoundingClientRect();
        return(
            rect.top >= 0 &&
            rect.left >= 0 &&
            rect.bottom <= (window.innerHeight || document. documentElement.clientHeight) && 
            rect.right <= (window.innerWidth || document. documentElement.clientWidth)
            );
}

交换topbottom,以及交换leftright

function isElementInViewport(el){
    var rect = el.getBoundingClientRect();
    return rect.bottom >= 0 &&
        rect.right >= 0 &&
        rect.top <= (window.innerHeight || document. documentElement.clientHeight) && 
        rect.left <= (window.innerWidth || document. documentElement.clientWidth);
}

此处演示:jsfiddle.net/w7ApB