pref: optimize virtual scroll (#111)

This commit is contained in:
Hayden
2025-02-02 16:42:25 +08:00
committed by GitHub
parent 448ea4b1ba
commit 56a2deb4eb
4 changed files with 84 additions and 306 deletions

View File

@@ -1,13 +1,6 @@
import { throttle } from 'lodash'
import { type Ref, onUnmounted, ref, toRef, watch } from 'vue'
export const defineResizeCallback = (
callback: ResizeObserverCallback,
wait?: number,
) => {
return throttle(callback, wait ?? 100)
}
export const useContainerResize = (
el: HTMLElement | null | Ref<HTMLElement | null>,
) => {
@@ -20,11 +13,11 @@ export const useContainerResize = (
toRef(el),
(el) => {
if (el) {
const onResize = defineResizeCallback((entries) => {
const onResize = throttle((entries: ResizeObserverEntry[]) => {
const entry = entries[0]
width.value = entry.contentRect.width
height.value = entry.contentRect.height
})
}, 64)
observer.value = new ResizeObserver(onResize)
observer.value.observe(el)

View File

@@ -22,29 +22,29 @@ export const useContainerScroll = (
scrollLeft.value = container.value.scrollLeft
scrollTop.value = container.value.scrollTop
}
}, options?.throttle || 100)
}, options?.throttle ?? 64)
watch(
container,
(el) => {
if (el) {
el.addEventListener('scroll', onScroll)
el.addEventListener('scroll', onScroll, { passive: true })
}
},
{ immediate: true },
)
const x = computed({
get: () => scrollLeft,
get: () => scrollLeft.value,
set: (val) => {
container.value?.scrollTo({ left: val.value })
container.value?.scrollTo({ left: val })
},
})
const y = computed({
get: () => scrollTop,
get: () => scrollTop.value,
set: (val) => {
container.value?.scrollTo({ top: val.value })
container.value?.scrollTo({ top: val })
},
})