blob: 00e12929880c349ce3d8d52cb52e21b471465639 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
// Returns true if element's center is within the visual viewport bounds.
function isInViewport(element) {
const viewportRect = {
left: visualViewport.offsetLeft,
top: visualViewport.offsetTop,
right: visualViewport.offsetLeft + visualViewport.width,
bottom: visualViewport.offsetTop + visualViewport.height
};
const elementRect = element.getBoundingClientRect();
const elementCenter = {
x: elementRect.left + elementRect.width / 2,
y: elementRect.top + elementRect.height / 2
};
return elementCenter.x > viewportRect.left &&
elementCenter.x < viewportRect.right &&
elementCenter.y > viewportRect.top &&
elementCenter.y < viewportRect.bottom;
}
|