Skip to content

Commit 022bf0d

Browse files
committed
feat: 优化
1 parent 97c9f63 commit 022bf0d

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

components/virtual-scroller/virtual-scroller.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ export const virtualScrollerProps = {
5555
renderItemList: {
5656
type: Function as PropType<(itemVNodes: VNodeChild) => VNodeChild>,
5757
},
58+
topThreshold: {
59+
type: Number,
60+
default: 0,
61+
},
62+
bottomThreshold: {
63+
type: Number,
64+
default: 0,
65+
},
5866
} as const satisfies ComponentObjectPropsOptions;
5967

6068
export type VirtualScrollerProps = ExtractPublicPropTypes<typeof virtualScrollerProps>;
@@ -139,15 +147,20 @@ export default defineComponent({
139147
getClientSize,
140148
});
141149

150+
let lastScrollTop = 0;
142151
const onScroll = (e: Event) => {
143152
emit('scroll', e);
153+
const currentScrollTop = (e.target as HTMLElement).scrollTop;
154+
const isScrollUp = currentScrollTop < lastScrollTop;
155+
lastScrollTop = currentScrollTop;
156+
144157
const offset = getOffset();
145158
const scrollSize = getScrollSize();
146159
const clientSize = getClientSize();
147-
if (offset === 0) {
160+
if (isScrollUp && offset <= props.topThreshold) {
148161
emit('toTop');
149162
}
150-
if (offset + clientSize >= scrollSize) {
163+
if (!isScrollUp && offset + clientSize >= scrollSize - props.bottomThreshold) {
151164
emit('toBottom');
152165
}
153166
};

docs/.vitepress/components/virtualScroller/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ scroll.vue
4949
| wrapTag | 列表包裹元素名称 | string | `div` |
5050
| wrapClass | 列表包裹元素类名 | string | - |
5151
| wrapStyle | 列表包裹元素内联样式 | object | `{}` |
52+
| topThreshold | 触发`toTop` 事件的阈值 | number | 0 |
53+
| bottomThreshold | 触发`toBottom` 事件的阈值 | number | 0 |
5254
| scrollbarProps | 滚动条样式,参考滚动条组件 | object | - |
5355

5456
## VirtualScroller Events

0 commit comments

Comments
 (0)