JS判断dom元素是否在可视范围内

dhso
2019/02/27 11:10
统计中
function isElementInViewport (el, offset = 0) {
    const box = el.getBoundingClientRect(),
          top = (box.top >= 0),
          left = (box.left >= 0),
          bottom = (box.bottom <= (window.innerHeight || document.documentElement.clientHeight) + offset),
          right = (box.right <= (window.innerWidth || document.documentElement.clientWidth) + offset);
    return (top && left && bottom && right);
}

本文为 dhso 原创

发布在 http://blog.minws.com/jspan-duan-domyuan-su-shi-fou-zai-ke-shi-fan-wei-nei/

如有转载,请标明来源!

作者信息
姓名:dhso
热评文章
最新评论
文章概览