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

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

2019-02-27 / 0 评论 / 577 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2021年10月27日,已超过884天没有更新,若内容或图片失效,请留言反馈。
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);
}

0

评论 (0)

取消