UNPKG

607 BJavaScriptView Raw
1/**
2 * @param {Element} el - 元素节点
3 * @param {Boolean} partiallyVisible - 是否部分出现
4 */
5const elementIsVisibleInViewport = (el, partiallyVisible = false) => {
6 const { top, left, bottom, right } = el.getBoundingClientRect();
7 const { innerHeight, innerWidth } = window;
8 return partiallyVisible
9 ? ((top > 0 && top < innerHeight) ||
10 (bottom > 0 && bottom < innerHeight)) &&
11 ((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth))
12 : top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth;
13};
14
15export default elementIsVisibleInViewport;