function initLazyImg() {
const imgs = document.querySelectorAll('.lazy-img[data-src]');
const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
if (img.dataset.src) {
img.src = img.dataset.src;
}
observer.unobserve(img);
}
});
});
imgs.forEach(img => {
if (!img.dataset.lazyLoaded) {
img.dataset.lazyLoaded = '1';
observer.observe(img);
}
});
}
// 页面首次加载
document.addEventListener('DOMContentLoaded', function () {
initLazyImg();
});
// 只监听视频列表区域
document.querySelectorAll('.myui-vodbox').forEach(box => {
const mo = new MutationObserver(function () {
initLazyImg();
});
mo.observe(box, {
childList: true,
subtree: true
});
});