判断元素是否在可视区域内

来源:互联网 发布:免费海关数据 编辑:程序博客网 时间:2024/05/19 09:17

getBoundingClientRect()

 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);    }
原创粉丝点击