Skip to content

Commit 9649aac

Browse files
kolarorzapathiaX
authored andcommitted
feat: 优化到达顶部底部的判断
1 parent 3e84823 commit 9649aac

File tree

4 files changed

+27
-23
lines changed

4 files changed

+27
-23
lines changed

docs/demos/chat/Main.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,8 @@ export default {
9191
},
9292
methods: {
9393
async toTop() {
94-
console.log('toTop');
9594
if (this.loading || this.page <= 1) return;
96-
95+
console.log('toTop');
9796
this.loading = true;
9897
9998
const list = await asyncGetList(

docs/demos/infinity/Main.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export default {
6868
data() {
6969
return {
7070
visible: true,
71+
loading: false,
7172
list: [] as any[],
7273
reactiveData: {
7374
renderBegin: 0,
@@ -83,9 +84,12 @@ export default {
8384
},
8485
methods: {
8586
async toBottom() {
87+
if (this.loading) return;
8688
console.log('toBottom');
89+
this.loading = true;
8790
const list = await asyncGetList(200, this.list.length, 1000);
8891
this.list = this.list.concat(list);
92+
this.loading = false;
8993
},
9094
},
9195
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-virt-list",
3-
"version": "1.5.10",
3+
"version": "1.5.11",
44
"description": "Tiny & Virtual scroll list & Huge amount data & High performance (support vue2.x&vue3.x)",
55
"author": "kolarorz",
66
"license": "MIT",

src/components/virt-list/index.tsx

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,26 @@ function useVirtList<T extends Record<string, any>>(
407407
}
408408
}
409409

410+
function judgePosition() {
411+
// 使用2px作为误差范围
412+
const threshold = Math.max(props.scrollDistance, 2);
413+
414+
if (direction === 'forward') {
415+
if (reactiveData.offset - threshold <= 0) {
416+
// console.log('[VirtList] 到达顶部');
417+
emitFunction?.toTop?.(props.list[0]);
418+
}
419+
} else if (direction === 'backward') {
420+
// 使用一个 Math.round 来解决小数点的误差问题
421+
const scrollSize = Math.round(reactiveData.offset + slotSize.clientSize);
422+
const distanceToBottom = Math.round(getTotalSize() - scrollSize);
423+
if (distanceToBottom <= threshold) {
424+
// console.log('[VirtList] 到达底部');
425+
emitFunction?.toBottom?.(props.list[props.list.length - 1]);
426+
}
427+
}
428+
}
429+
410430
function onScroll(evt: Event) {
411431
// console.log('onscroll');
412432

@@ -420,26 +440,7 @@ function useVirtList<T extends Record<string, any>>(
420440

421441
calcRange();
422442

423-
// 到达顶部
424-
if (
425-
direction === 'forward' &&
426-
reactiveData.offset - props.scrollDistance <= 0
427-
) {
428-
// console.log('[VirtList] 到达顶部');
429-
emitFunction?.toTop?.(props.list[0]);
430-
}
431-
// 到达底部 - 放在这里是为了渲染完成拿到真是高度了,再判断是否是真的到达底部
432-
// 使用一个 Math.round 来解决小数点的误差问题
433-
if (
434-
direction === 'backward' &&
435-
Math.round(reactiveData.offset + props.scrollDistance) >=
436-
Math.round(
437-
reactiveData.listTotalSize + getSlotSize() - slotSize.clientSize,
438-
)
439-
) {
440-
// console.log('[VirtList] 到达底部');
441-
emitFunction?.toBottom?.(props.list[props.list.length - 1]);
442-
}
443+
judgePosition();
443444
}
444445

445446
function calcViews() {

0 commit comments

Comments
 (0)