Skip to content

Commit f1e3d77

Browse files
committed
懒加载图片、LoadingCover样式
1 parent c68e7c9 commit f1e3d77

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

components/LazyImage.js

+22-5
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ export default function LazyImage({
5959
useEffect(() => {
6060
const adjustedImageSrc =
6161
adjustImgSize(src, maxWidth) || defaultPlaceholderSrc
62-
6362
// 加载原图
6463
const img = new Image()
6564
img.src = adjustedImageSrc
@@ -68,7 +67,6 @@ export default function LazyImage({
6867
handleImageLoaded(adjustedImageSrc)
6968
}
7069
img.onerror = handleImageError
71-
7270
const observer = new IntersectionObserver(
7371
entries => {
7472
entries.forEach(entry => {
@@ -81,11 +79,9 @@ export default function LazyImage({
8179
},
8280
{ rootMargin: '50px 0px' } // Adjust the rootMargin as needed to trigger the loading earlier or later
8381
)
84-
8582
if (imageRef.current) {
8683
observer.observe(imageRef.current)
8784
}
88-
8985
return () => {
9086
if (imageRef.current) {
9187
observer.unobserve(imageRef.current)
@@ -97,6 +93,7 @@ export default function LazyImage({
9793
const imgProps = {
9894
ref: imageRef,
9995
src: currentSrc,
96+
'data-src': src,
10097
alt: alt,
10198
onLoad: handleThumbnailLoaded, // 缩略图加载完成
10299
onError: handleImageError // 添加onError处理函数
@@ -118,14 +115,19 @@ export default function LazyImage({
118115
imgProps.height = height
119116
}
120117
if (className) {
121-
imgProps.className = className
118+
imgProps.className = className + ' lazy-image-placeholder'
122119
}
123120
if (style) {
124121
imgProps.style = style
125122
}
126123
if (onClick) {
127124
imgProps.onClick = onClick
128125
}
126+
127+
if (!src) {
128+
return null
129+
}
130+
129131
return (
130132
<>
131133
{/* eslint-disable-next-line @next/next/no-img-element */}
@@ -136,6 +138,21 @@ export default function LazyImage({
136138
<link rel='preload' as='image' href={adjustImgSize(src, maxWidth)} />
137139
</Head>
138140
)}
141+
<style>
142+
{`
143+
.lazy-image-placeholder{
144+
145+
background:
146+
linear-gradient(90deg,#0001 33%,#0005 50%,#0001 66%)
147+
#f2f2f2;
148+
background-size:300% 100%;
149+
animation: l1 1s infinite linear;
150+
}
151+
@keyframes l1 {
152+
0% {background-position: right}
153+
}
154+
`}
155+
</style>
139156
</>
140157
)
141158
}

components/LoadingCover.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'user client'
12
import { useGlobal } from '@/lib/global'
23
import { useEffect, useState } from 'react'
34
/**

lib/global.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function GlobalContextProvider(props) {
3939

4040
const defaultDarkMode = NOTION_CONFIG?.APPEARANCE || APPEARANCE
4141
const [isDarkMode, updateDarkMode] = useState(defaultDarkMode === 'dark') // 默认深色模式
42-
const [onLoading, setOnLoading] = useState(true) // 抓取文章数据
42+
const [onLoading, setOnLoading] = useState(false) // 抓取文章数据
4343
const router = useRouter()
4444

4545
// 登录验证相关

0 commit comments

Comments
 (0)