update code level
This commit is contained in:
@@ -443,36 +443,36 @@ function updateLightboxContent(newIndex) {
|
||||
|
||||
// 初始化图片懒加载
|
||||
function initLazyLoading() {
|
||||
// 使用Intersection Observer实现懒加载
|
||||
if ('IntersectionObserver' in window) {
|
||||
const imageObserver = new IntersectionObserver((entries, observer) => {
|
||||
const observer = new IntersectionObserver((entries) => {
|
||||
entries.forEach(entry => {
|
||||
if (entry.isIntersecting) {
|
||||
const img = entry.target;
|
||||
if (img.dataset.src) {
|
||||
img.src = img.dataset.src;
|
||||
img.classList.remove('lazy');
|
||||
delete img.dataset.src;
|
||||
const imageId = img.getAttribute('data-image-id');
|
||||
|
||||
if (imageId && !img.classList.contains('loaded')) {
|
||||
imageLoader.loadImage(imageId, img);
|
||||
}
|
||||
imageObserver.unobserve(img);
|
||||
|
||||
observer.unobserve(img);
|
||||
}
|
||||
});
|
||||
}, {
|
||||
rootMargin: '50px 0px',
|
||||
rootMargin: '200px 0px', // 提前200px开始加载
|
||||
threshold: 0.1
|
||||
});
|
||||
|
||||
document.querySelectorAll('img.lazy').forEach(img => {
|
||||
imageObserver.observe(img);
|
||||
});
|
||||
} else {
|
||||
// 不支持IntersectionObserver的浏览器回退方案
|
||||
document.querySelectorAll('img.lazy').forEach(img => {
|
||||
if (img.dataset.src) {
|
||||
img.src = img.dataset.src;
|
||||
img.classList.remove('lazy');
|
||||
delete img.dataset.src;
|
||||
// 观察所有带有data-image-id的图片
|
||||
document.querySelectorAll('img[data-image-id]').forEach(img => {
|
||||
if (!img.classList.contains('loaded')) {
|
||||
observer.observe(img);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// 不支持Intersection Observer的回退方案
|
||||
console.warn('浏览器不支持Intersection Observer,使用回退懒加载方案');
|
||||
initAlbumFallbackLazyLoading();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user