File: /var/www/quadcode.com/src/utils/lazyVideo.ts
const lazyVideo = (el: HTMLElement | null) => {
const video = el;
if (!window?.['IntersectionObserver']) {
return;
}
const lazyVideoObserver = new IntersectionObserver(function (entries) {
entries.forEach(function (video) {
if (video.isIntersecting) {
for (let i = 0; i < video.target.children?.length; ++i) {
if (!video.target.children[i]) {
return;
}
video.target.children[i].src = video.target.children[i]?.dataset?.src || '';
}
video.target.load();
lazyVideoObserver.unobserve(video.target);
}
});
});
if (!video) {
return;
}
lazyVideoObserver.observe(video);
};
export default lazyVideo;