JavaScript判断元素是否在工作窗口内

function isInViewPortOfOne (el) {
    const viewPortHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight ;//窗口可视高度
    const offsetTop = el.offsetTop;//元素顶部高度
    const offsetHeight = el.offsetHeight;//元素高度
    const scrollTop = document.documentElement.scrollTop;//滚动距离
    //判断是否在工作窗口内
    const top = (offsetTop+offsetHeight )- scrollTop
    if(top>0&&top<viewPortHeight){
        return true
    }
    else{
        return false;
    }
}

猜你喜欢

转载自blog.csdn.net/baidu_33548663/article/details/88182671