From 6cc2d85787171281c269f4a6c3290b4a0762bcb6 Mon Sep 17 00:00:00 2001 From: YangYuGang <1378265336@qq.com> Date: 星期三, 05 三月 2025 16:21:43 +0800 Subject: [PATCH] 生产工具 --- src/components/VirtualScroll/src/VirtualScroll.vue | 38 +++++++++++++++++++++++++++++++++++--- 1 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/components/VirtualScroll/src/VirtualScroll.vue b/src/components/VirtualScroll/src/VirtualScroll.vue index 969ce13..12080cb 100644 --- a/src/components/VirtualScroll/src/VirtualScroll.vue +++ b/src/components/VirtualScroll/src/VirtualScroll.vue @@ -11,8 +11,8 @@ CSSProperties, PropType, } from 'vue'; - import { useEventListener } from '/@/hooks/event/useEventListener'; - import { getSlot } from '/@/utils/helper/tsxHelper'; + import { useEventListener } from '@/hooks/event/useEventListener'; + import { getSlot } from '@/utils/helper/tsxHelper'; type NumberOrNumberString = PropType<string | number | undefined>; @@ -52,7 +52,7 @@ export default defineComponent({ name: 'VirtualScroll', props, - setup(props, { slots }) { + setup(props, { slots, expose }) { const wrapElRef = ref<HTMLDivElement | null>(null); const state = reactive({ first: 0, @@ -128,6 +128,31 @@ state.last = getLast(state.first); } + function scrollToTop() { + const wrapEl = unref(wrapElRef); + if (!wrapEl) { + return; + } + wrapEl.scrollTop = 0; + } + + function scrollToBottom() { + const wrapEl = unref(wrapElRef); + if (!wrapEl) { + return; + } + wrapEl.scrollTop = wrapEl.scrollHeight; + } + + function scrollToItem(index: number) { + const wrapEl = unref(wrapElRef); + if (!wrapEl) { + return; + } + const i = index - 1 > 0 ? index - 1 : 0; + wrapEl.scrollTop = i * unref(getItemHeightRef); + } + function renderChildren() { const { items = [] } = props; return items.slice(unref(getFirstToRenderRef), unref(getLastToRenderRef)).map(genChild); @@ -143,6 +168,13 @@ ); } + expose({ + wrapElRef, + scrollToTop, + scrollToItem, + scrollToBottom, + }); + onMounted(() => { state.last = getLast(0); nextTick(() => { -- Gitblit v1.9.3