Skip to content

Commit 839b934

Browse files
committed
Add direction property to support load data when scroll to top
1 parent a96468e commit 839b934

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/components/InfiniteLoading.vue

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,27 @@
3939
/**
4040
* get current distance from footer
4141
* @param {DOM} elm scroll element
42+
* @param {String} dir calculate direction
4243
* @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);
55-
56-
return scrollHeight - innerHeight - scrollTop - paddingTop - paddingBottom;
48+
if (dir === 'top') {
49+
distance = scrollTop;
50+
} else {
51+
const styles = getComputedStyle(elm === window ? document.body : elm);
52+
const innerHeight = elm === window
53+
? window.innerHeight
54+
: parseInt(styles.height, 10);
55+
const scrollHeight = elm === window
56+
? document.body.scrollHeight
57+
: elm.scrollHeight;
58+
const paddingTop = parseInt(styles.paddingTop, 10);
59+
const paddingBottom = parseInt(styles.paddingBottom, 10);
60+
distance = scrollHeight - innerHeight - scrollTop - paddingTop - paddingBottom;
61+
}
62+
return distance;
5763
}
5864
5965
export default {
@@ -78,6 +84,10 @@
7884
},
7985
onInfinite: Function,
8086
spinner: String,
87+
direction: {
88+
type: String,
89+
default: 'bottom',
90+
},
8191
},
8292
mounted() {
8393
this.scrollParent = getScrollParent(this.$el);

0 commit comments

Comments
 (0)