Skip to content

Commit 89bf487

Browse files
committed
Improve logic for the getCurrentDistance function
1 parent 1ad9578 commit 89bf487

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
@@ -39,27 +39,24 @@
3939
}
4040
4141
/**
42-
* get current distance from footer
43-
* @param {DOM} elm scroll element
44-
* @param {String} dir calculate direction
42+
* get current distance from bottom
43+
* @param {DOM} scrollElm scroll element
44+
* @param {DOM} infiniteElm infinite element
45+
* @param {String} dir calculate direction
4546
* @return {Number} distance
4647
*/
47-
function getCurrentDistance(elm, dir) {
48+
function getCurrentDistance(scrollElm, infiniteElm, dir) {
4849
let distance;
4950
5051
if (dir === 'top') {
51-
distance = isNaN(elm.scrollTop) ? elm.pageYOffset : elm.scrollTop;
52+
distance = isNaN(scrollElm.scrollTop) ? scrollElm.pageYOffset : scrollElm.scrollTop;
5253
} else {
53-
let scrollElmHeight;
54-
let elOffsetTop = this.$el.getBoundingClientRect().top;
54+
const infiniteElmOffsetTopFromBottom = infiniteElm.getBoundingClientRect().top;
55+
const scrollElmOffsetTopFromBottom = scrollElm === window ?
56+
window.innerHeight :
57+
scrollElm.getBoundingClientRect().bottom;
5558
56-
if (elm === window) {
57-
scrollElmHeight = window.innerHeight;
58-
} else {
59-
scrollElmHeight = elm.clientHeight;
60-
elOffsetTop -= elm.getBoundingClientRect().top;
61-
}
62-
distance = elOffsetTop - scrollElmHeight;
59+
distance = infiniteElmOffsetTopFromBottom - scrollElmOffsetTopFromBottom;
6360
}
6461
6562
return distance;
@@ -131,7 +128,7 @@
131128
},
132129
methods: {
133130
attemptLoad() {
134-
const currentDistance = getCurrentDistance.bind(this)(this.scrollParent, this.direction);
131+
const currentDistance = getCurrentDistance(this.scrollParent, this.$el, this.direction);
135132
if (!this.isComplete && currentDistance <= this.distance) {
136133
this.isLoading = true;
137134
this.onInfinite.call();

0 commit comments

Comments
 (0)