Skip to content

Commit 7a14d81

Browse files
committed
Modify the way to save scroll instances in infinite loading component
1 parent 85b6b6c commit 7a14d81

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/components/InfiniteLoading.vue

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
</div>
55
</template>
66
<script>
7-
let scrollParent;
8-
let scrollHandler;
9-
107
/**
118
* get the first scroll parent of an element
129
* @param {DOM} elm the element which find scorll parent
@@ -42,6 +39,8 @@
4239
data() {
4340
return {
4441
isLoading: false,
42+
scrollParent: null,
43+
scrollHandler: null,
4544
};
4645
},
4746
props: {
@@ -53,22 +52,20 @@
5352
this.$set('distance', 100);
5453
}
5554
56-
scrollParent = getScrollParent(this.$el);
55+
this.scrollParent = getScrollParent(this.$el);
5756
58-
scrollHandler = function scrollHandlerOriginal() {
59-
const currentDistance = getCurrentDistance(scrollParent);
60-
if (!this.isLoading) {
61-
if (currentDistance <= this.distance) {
62-
this.isLoading = true;
63-
if (this.onInfinite) {
64-
this.onInfinite.call();
65-
}
57+
this.scrollHandler = function scrollHandlerOriginal() {
58+
const currentDistance = getCurrentDistance(this.scrollParent);
59+
if (!this.isLoading && currentDistance <= this.distance) {
60+
this.isLoading = true;
61+
if (this.onInfinite) {
62+
this.onInfinite.call();
6663
}
6764
}
6865
}.bind(this);
6966
70-
setTimeout(scrollHandler, 1);
71-
scrollParent.addEventListener('scroll', scrollHandler);
67+
setTimeout(this.scrollHandler, 1);
68+
this.scrollParent.addEventListener('scroll', this.scrollHandler);
7269
},
7370
events: {
7471
// Hide the loading icon when data was loaded
@@ -77,7 +74,7 @@
7774
},
7875
},
7976
destroyed() {
80-
scrollParent.removeEventListener('scroll', scrollHandler);
77+
this.scrollParent.removeEventListener('scroll', this.scrollHandler);
8178
},
8279
};
8380
</script>

0 commit comments

Comments
 (0)