Skip to content

Commit 3e693a1

Browse files
author
zhlhlf
committed
实现Grid View 根据视野位置加载
1 parent 0482c96 commit 3e693a1

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/pages/home/folder/GridItem.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Center, VStack, Icon, Text } from "@hope-ui/solid"
22
import { Motion } from "solid-motionone"
33
import { useContextMenu } from "solid-contextmenu"
4-
import { batch, Show } from "solid-js"
4+
import { batch, Show, createSignal, onMount, onCleanup } from "solid-js"
55
import { CenterLoading, LinkWithPush, ImageWithError } from "~/components"
66
import { usePath, useRouter, useUtil } from "~/hooks"
77
import { checkboxOpen, getMainColor, local, selectIndex } from "~/store"
@@ -27,8 +27,25 @@ export const GridItem = (props: { obj: StoreObj; index: number }) => {
2727
const { pushHref, to } = useRouter()
2828
const { openWithDoubleClick, toggleWithClick, restoreSelectionCache } =
2929
useSelectWithMouse()
30+
31+
const [isVisible, setIsVisible] = createSignal(false)
32+
let ref: HTMLDivElement | undefined
33+
34+
onMount(() => {
35+
if (ref) {
36+
const observer = new IntersectionObserver(
37+
([entry]) => {
38+
setIsVisible(entry.isIntersecting)
39+
},
40+
{ rootMargin: "50px" },
41+
)
42+
observer.observe(ref)
43+
onCleanup(() => observer.disconnect())
44+
}
45+
})
3046
return (
3147
<Motion.div
48+
ref={ref}
3249
initial={{ opacity: 0, scale: 0.9 }}
3350
animate={{ opacity: 1, scale: 1 }}
3451
transition={{ duration: 0.2 }}
@@ -115,7 +132,7 @@ export const GridItem = (props: { obj: StoreObj; index: number }) => {
115132
}}
116133
/>
117134
</Show>
118-
<Show when={props.obj.thumb} fallback={objIcon}>
135+
<Show when={props.obj.thumb && isVisible()} fallback={objIcon}>
119136
<ImageWithError
120137
maxH="$full"
121138
maxW="$full"

0 commit comments

Comments
 (0)