Skip to content

Commit b683c00

Browse files
author
zhlhlf
committed
实现加载图片的时候延迟500ms 防止快速划过不关注的地方也在加载阻塞滑到的地方
1 parent 3e693a1 commit b683c00

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/components/ImageWithError.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import { createSignal, JSXElement, Show } from "solid-js"
44
export const ImageWithError = <C extends ElementType = "img">(
55
props: ImageProps<C> & {
66
fallbackErr?: JSXElement
7+
onLoad?: () => void
78
},
89
) => {
910
const [err, setErr] = createSignal(false)
1011
return (
1112
<Show when={!err()} fallback={props.fallbackErr}>
1213
<Image
1314
{...props}
15+
onLoad={props.onLoad}
1416
onError={() => {
1517
setErr(true)
1618
}}

src/pages/home/folder/GridItem.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,30 @@ export const GridItem = (props: { obj: StoreObj; index: number }) => {
2929
useSelectWithMouse()
3030

3131
const [isVisible, setIsVisible] = createSignal(false)
32+
const [loaded, setLoaded] = createSignal(false)
33+
const [canLoad, setCanLoad] = createSignal(false)
3234
let ref: HTMLDivElement | undefined
35+
let loadTimeout: number | undefined
3336

3437
onMount(() => {
3538
if (ref) {
3639
const observer = new IntersectionObserver(
3740
([entry]) => {
41+
if (entry.isIntersecting) {
42+
loadTimeout = setTimeout(() => setCanLoad(true), 500)
43+
} else {
44+
if (loadTimeout) clearTimeout(loadTimeout)
45+
setCanLoad(false)
46+
}
3847
setIsVisible(entry.isIntersecting)
3948
},
4049
{ rootMargin: "50px" },
4150
)
4251
observer.observe(ref)
43-
onCleanup(() => observer.disconnect())
52+
onCleanup(() => {
53+
observer.disconnect()
54+
if (loadTimeout) clearTimeout(loadTimeout)
55+
})
4456
}
4557
})
4658
return (
@@ -132,7 +144,10 @@ export const GridItem = (props: { obj: StoreObj; index: number }) => {
132144
}}
133145
/>
134146
</Show>
135-
<Show when={props.obj.thumb && isVisible()} fallback={objIcon}>
147+
<Show
148+
when={props.obj.thumb && (canLoad() || loaded())}
149+
fallback={objIcon}
150+
>
136151
<ImageWithError
137152
maxH="$full"
138153
maxW="$full"
@@ -142,6 +157,7 @@ export const GridItem = (props: { obj: StoreObj; index: number }) => {
142157
fallbackErr={objIcon}
143158
src={props.obj.thumb}
144159
loading="lazy"
160+
onLoad={() => setLoaded(true)}
145161
/>
146162
</Show>
147163
</Center>

0 commit comments

Comments
 (0)