Skip to content

Commit fcdd60e

Browse files
authored
fix: final load jump when top is true (#71)
* Fix a bug in InfiniteLoading.vue that causes the final load to jump to the top of the elements when `top` is `true`. * Updated InfiniteLoading.vue to make the `complete` function async. * Moved the scroll position logic to a utility function.
1 parent 670bde2 commit fcdd60e

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/components/InfiniteLoading.vue

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts" setup>
22
import type { Props, Params, State, StateHandler } from "@root/types";
33
import { onMounted, ref, toRefs, onUnmounted, watch, nextTick } from "vue";
4-
import { startObserver, getParentEl, isVisible } from "@root/utils";
4+
import { startObserver, getParentEl, isVisible, updateScrollPosition } from "@root/utils";
55
// @ts-ignore
66
import Spinner from "./Spinner.vue";
77
@@ -44,13 +44,12 @@ const stateHandler: StateHandler = {
4444
},
4545
async loaded() {
4646
state.value = "loaded";
47-
const parentEl = params.parentEl || document.documentElement;
48-
await nextTick();
49-
if (top) parentEl.scrollTop = parentEl.scrollHeight - prevHeight;
47+
if (top) updateScrollPosition(params, prevHeight);
5048
if (isVisible(infiniteLoading.value!, params.parentEl)) params.emit();
5149
},
52-
complete() {
50+
async complete() {
5351
state.value = "complete";
52+
if (top) updateScrollPosition(params, prevHeight);
5453
observer?.disconnect();
5554
},
5655
error() {

src/utils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,10 @@ function startObserver(params: Params) {
3232
return observer;
3333
}
3434

35-
export { startObserver, isVisible, getParentEl };
35+
async function updateScrollPosition(params: Params, prevHeight: number) {
36+
const parentEl = params.parentEl || document.documentElement;
37+
await nextTick();
38+
parentEl.scrollTop = parentEl.scrollHeight - prevHeight;
39+
}
40+
41+
export { startObserver, isVisible, getParentEl, updateScrollPosition };

0 commit comments

Comments
 (0)