|
38 | 38 |
|
39 | 39 | /**
|
40 | 40 | * 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 |
43 | 44 | */
|
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; |
52 | 47 | const scrollTop = isNaN(elm.scrollTop) ? elm.pageYOffset : elm.scrollTop;
|
53 |
| - const paddingTop = parseInt(styles.paddingTop, 10); |
54 |
| - const paddingBottom = parseInt(styles.paddingBottom, 10); |
55 | 48 |
|
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; |
57 | 66 | }
|
58 | 67 |
|
59 | 68 | export default {
|
|
81 | 90 | required: true,
|
82 | 91 | },
|
83 | 92 | spinner: String,
|
| 93 | + direction: { |
| 94 | + type: String, |
| 95 | + default: 'bottom', |
| 96 | + }, |
84 | 97 | },
|
85 | 98 | ready() {
|
86 | 99 | this.scrollParent = getScrollParent(this.$el);
|
|
116 | 129 | },
|
117 | 130 | methods: {
|
118 | 131 | attemptLoad() {
|
119 |
| - const currentDistance = getCurrentDistance(this.scrollParent); |
| 132 | + const currentDistance = getCurrentDistance(this.scrollParent, this.direction); |
120 | 133 | if (!this.isComplete && currentDistance <= this.distance) {
|
121 | 134 | this.isLoading = true;
|
122 | 135 | this.onInfinite.call();
|
|
0 commit comments