From f3320bd59b075652377dc648c1b1e90bacb5dec2 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Sat, 23 Nov 2024 10:50:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E7=89=87=E6=87=92=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E8=B0=83=E4=BC=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/LazyImage.js | 68 ++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 41 deletions(-) diff --git a/components/LazyImage.js b/components/LazyImage.js index acfb89c1..8543dbb9 100644 --- a/components/LazyImage.js +++ b/components/LazyImage.js @@ -57,32 +57,35 @@ export default function LazyImage({ } else { imageRef.current.src = defaultPlaceholderSrc } - imageRef.current.classList.remove('lazy-image-placeholder') + // 移除占位符类名 + if (imageRef.current) { + imageRef.current.classList.remove('lazy-image-placeholder') + } } } useEffect(() => { const adjustedImageSrc = adjustImgSize(src, maxWidth) || defaultPlaceholderSrc - // 加载原图 - const img = new Image() - img.src = adjustedImageSrc - img.onload = () => { - setCurrentSrc(adjustedImageSrc) - handleImageLoaded(adjustedImageSrc) - } - img.onerror = handleImageError + const observer = new IntersectionObserver( entries => { entries.forEach(entry => { if (entry.isIntersecting) { - const lazyImage = entry.target - lazyImage.src = adjustedImageSrc - observer.unobserve(lazyImage) + // 拉取图片 + const img = new Image() + img.src = adjustedImageSrc + img.onload = () => { + setCurrentSrc(adjustedImageSrc) + handleImageLoaded(adjustedImageSrc) + } + img.onerror = handleImageError + + observer.unobserve(entry.target) } }) }, - { rootMargin: '50px 0px' } // Adjust the rootMargin as needed to trigger the loading earlier or later + { rootMargin: '10px 0px' } // 轻微提前加载 ) if (imageRef.current) { observer.observe(imageRef.current) @@ -98,36 +101,19 @@ export default function LazyImage({ const imgProps = { ref: imageRef, src: currentSrc, - 'data-src': src, - alt: alt, - onLoad: handleThumbnailLoaded, // 缩略图加载完成 - onError: handleImageError // 添加onError处理函数 + 'data-src': src, // 存储原始图片地址 + alt: alt || 'Lazy loaded image', + onLoad: handleThumbnailLoaded, + onError: handleImageError, + className: `${className || ''} lazy-image-placeholder`, + style, + width: width || 'auto', + height: height || 'auto', + onClick } - if (id) { - imgProps.id = id - } - - if (title) { - imgProps.title = title - } - - if (width && width !== 'auto') { - imgProps.width = width - } - - if (height && height !== 'auto') { - imgProps.height = height - } - if (className) { - imgProps.className = className + ' lazy-image-placeholder' - } - if (style) { - imgProps.style = style - } - if (onClick) { - imgProps.onClick = onClick - } + if (id) imgProps.id = id + if (title) imgProps.title = title if (!src) { return null