Skip to content

Commit bf23c43

Browse files
committed
Add direction property to support load data when scroll to top
1 parent e05eef6 commit bf23c43

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

src/components/InfiniteLoading.vue

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,31 @@
3838
3939
/**
4040
* get current distance from footer
41-
* @param {DOM} elm scroll element
42-
* @return {Number} distance
41+
* @param {DOM} elm scroll element
42+
* @param {String} dir calculate direction
43+
* @return {Number} distance
4344
*/
44-
function getCurrentDistance(elm) {
45-
const styles = getComputedStyle(elm === window ? document.body : elm);
46-
const innerHeight = elm === window
47-
? window.innerHeight
48-
: parseInt(styles.height, 10);
49-
const scrollHeight = elm === window
50-
? document.body.scrollHeight
51-
: elm.scrollHeight;
45+
function getCurrentDistance(elm, dir) {
46+
let distance;
5247
const scrollTop = isNaN(elm.scrollTop) ? elm.pageYOffset : elm.scrollTop;
53-
const paddingTop = parseInt(styles.paddingTop, 10);
54-
const paddingBottom = parseInt(styles.paddingBottom, 10);
5548
56-
return scrollHeight - innerHeight - scrollTop - paddingTop - paddingBottom;
49+
if (dir === 'top') {
50+
distance = scrollTop;
51+
} else {
52+
const styles = getComputedStyle(elm === window ? document.body : elm);
53+
const innerHeight = elm === window
54+
? window.innerHeight
55+
: parseInt(styles.height, 10);
56+
const scrollHeight = elm === window
57+
? document.body.scrollHeight
58+
: elm.scrollHeight;
59+
const paddingTop = parseInt(styles.paddingTop, 10);
60+
const paddingBottom = parseInt(styles.paddingBottom, 10);
61+
62+
distance = scrollHeight - innerHeight - scrollTop - paddingTop - paddingBottom;
63+
}
64+
65+
return distance;
5766
}
5867
5968
export default {
@@ -81,6 +90,10 @@
8190
required: true,
8291
},
8392
spinner: String,
93+
direction: {
94+
type: String,
95+
default: 'bottom',
96+
},
8497
},
8598
ready() {
8699
this.scrollParent = getScrollParent(this.$el);
@@ -116,7 +129,7 @@
116129
},
117130
methods: {
118131
attemptLoad() {
119-
const currentDistance = getCurrentDistance(this.scrollParent);
132+
const currentDistance = getCurrentDistance(this.scrollParent, this.direction);
120133
if (!this.isComplete && currentDistance <= this.distance) {
121134
this.isLoading = true;
122135
this.onInfinite.call();

0 commit comments

Comments
 (0)