mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-28 15:10:04 +00:00
图片加载异常占位图
This commit is contained in:
@@ -20,6 +20,8 @@ export default function LazyImage({
|
|||||||
style
|
style
|
||||||
}) {
|
}) {
|
||||||
const maxWidth = siteConfig('IMAGE_COMPRESS_WIDTH')
|
const maxWidth = siteConfig('IMAGE_COMPRESS_WIDTH')
|
||||||
|
const defaultPlaceholderSrc = siteConfig('IMG_LAZY_LOAD_PLACEHOLDER')
|
||||||
|
|
||||||
const imageRef = useRef(null)
|
const imageRef = useRef(null)
|
||||||
const [adjustedSrc, setAdjustedSrc] = useState(
|
const [adjustedSrc, setAdjustedSrc] = useState(
|
||||||
placeholderSrc || siteConfig('IMG_LAZY_LOAD_PLACEHOLDER')
|
placeholderSrc || siteConfig('IMG_LAZY_LOAD_PLACEHOLDER')
|
||||||
@@ -37,6 +39,14 @@ export default function LazyImage({
|
|||||||
onLoad() // 触发传递的onLoad回调函数
|
onLoad() // 触发传递的onLoad回调函数
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 图片加载失败回调
|
||||||
|
*/
|
||||||
|
const handleImageError = () => {
|
||||||
|
if (imageRef.current) {
|
||||||
|
imageRef.current.src = defaultPlaceholderSrc
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const adjustedImageSrc = adjustImgSize(src, maxWidth)
|
const adjustedImageSrc = adjustImgSize(src, maxWidth)
|
||||||
@@ -71,7 +81,8 @@ export default function LazyImage({
|
|||||||
ref: imageRef,
|
ref: imageRef,
|
||||||
src: priority ? adjustedSrc : placeholderSrc,
|
src: priority ? adjustedSrc : placeholderSrc,
|
||||||
alt: alt,
|
alt: alt,
|
||||||
onLoad: handleImageLoad
|
onLoad: handleImageLoad,
|
||||||
|
onError: handleImageError // 添加onError处理函数
|
||||||
}
|
}
|
||||||
|
|
||||||
if (id) {
|
if (id) {
|
||||||
|
|||||||
Reference in New Issue
Block a user