16 lines
576 B
JavaScript
16 lines
576 B
JavaScript
|
|
// src/scripts/isElementInViewport.ts
|
||
|
|
function isElementInViewport(elem) {
|
||
|
|
if (!elem.getBoundingClientRect) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
const rect = elem.getBoundingClientRect();
|
||
|
|
const windowHeight = window.innerHeight || document.documentElement.clientHeight;
|
||
|
|
const windowWidth = window.innerWidth || document.documentElement.clientWidth;
|
||
|
|
const vertInView = rect.top < windowHeight && rect.top + rect.height > 0;
|
||
|
|
const horInView = rect.left < windowWidth && rect.left + rect.width > 0;
|
||
|
|
return vertInView && horInView;
|
||
|
|
}
|
||
|
|
export {
|
||
|
|
isElementInViewport as default
|
||
|
|
};
|