|
24 | 24 | waveDots: 'loading-wave-dots', |
25 | 25 | }; |
26 | 26 |
|
27 | | - /** |
28 | | - * get the first scroll parent of an element |
29 | | - * @param {DOM} elm the element which find scorll parent |
30 | | - * @return {DOM} the first scroll parent |
31 | | - */ |
32 | | - function getScrollParent(elm) { |
33 | | - if (elm.tagName === 'BODY') { |
34 | | - return window; |
35 | | - } else if (['scroll', 'auto'].indexOf(getComputedStyle(elm).overflowY) > -1) { |
36 | | - return elm; |
37 | | - } else if (elm.hasAttribute('infinite-wrapper') || elm.hasAttribute('data-infinite-wrapper')) { |
38 | | - return elm; |
39 | | - } |
40 | | - return getScrollParent(elm.parentNode); |
41 | | - } |
42 | | -
|
43 | | - /** |
44 | | - * get current distance from bottom |
45 | | - * @param {DOM} scrollElm scroll element |
46 | | - * @param {DOM} infiniteElm infinite element |
47 | | - * @param {String} dir calculate direction |
48 | | - * @return {Number} distance |
49 | | - */ |
50 | | - function getCurrentDistance(scrollElm, infiniteElm, dir) { |
51 | | - let distance; |
52 | | -
|
53 | | - if (dir === 'top') { |
54 | | - distance = isNaN(scrollElm.scrollTop) ? scrollElm.pageYOffset : scrollElm.scrollTop; |
55 | | - } else { |
56 | | - const infiniteElmOffsetTopFromBottom = infiniteElm.getBoundingClientRect().top; |
57 | | - const scrollElmOffsetTopFromBottom = scrollElm === window ? |
58 | | - window.innerHeight : |
59 | | - scrollElm.getBoundingClientRect().bottom; |
60 | | -
|
61 | | - distance = infiniteElmOffsetTopFromBottom - scrollElmOffsetTopFromBottom; |
62 | | - } |
63 | | -
|
64 | | - return distance; |
65 | | - } |
66 | | -
|
67 | 27 | export default { |
68 | 28 | data() { |
69 | 29 | return { |
|
112 | 72 | }, |
113 | 73 | }, |
114 | 74 | mounted() { |
115 | | - this.scrollParent = getScrollParent(this.$el); |
| 75 | + this.scrollParent = this.getScrollParent(); |
116 | 76 |
|
117 | 77 | this.scrollHandler = function scrollHandlerOriginal(ev) { |
118 | 78 | if (!this.isLoading) { |
|
170 | 130 | this.scrollParent.addEventListener('scroll', this.scrollHandler); |
171 | 131 | }, |
172 | 132 | methods: { |
| 133 | + /** |
| 134 | + * attempt trigger load |
| 135 | + */ |
173 | 136 | attemptLoad() { |
174 | | - const currentDistance = getCurrentDistance(this.scrollParent, this.$el, this.direction); |
| 137 | + const currentDistance = this.getCurrentDistance(); |
| 138 | +
|
175 | 139 | if (!this.isComplete && currentDistance <= this.distance) { |
176 | 140 | this.isLoading = true; |
177 | 141 |
|
|
184 | 148 | this.isLoading = false; |
185 | 149 | } |
186 | 150 | }, |
| 151 | + /** |
| 152 | + * get current distance from the specified direction |
| 153 | + * @return {Number} distance |
| 154 | + */ |
| 155 | + getCurrentDistance() { |
| 156 | + let distance; |
| 157 | +
|
| 158 | + if (this.direction === 'top') { |
| 159 | + distance = isNaN(this.scrollParent.scrollTop) ? |
| 160 | + this.scrollParent.pageYOffset : |
| 161 | + this.scrollParent.scrollTop; |
| 162 | + } else { |
| 163 | + const infiniteElmOffsetTopFromBottom = this.$el.getBoundingClientRect().top; |
| 164 | + const scrollElmOffsetTopFromBottom = this.scrollParent === window ? |
| 165 | + window.innerHeight : |
| 166 | + this.scrollParent.getBoundingClientRect().bottom; |
| 167 | +
|
| 168 | + distance = infiniteElmOffsetTopFromBottom - scrollElmOffsetTopFromBottom; |
| 169 | + } |
| 170 | +
|
| 171 | + return distance; |
| 172 | + }, |
| 173 | + /** |
| 174 | + * get the first scroll parent of an element |
| 175 | + * @param {DOM} elm cache element for recursive search |
| 176 | + * @return {DOM} the first scroll parent |
| 177 | + */ |
| 178 | + getScrollParent(elm = this.$el) { |
| 179 | + let result; |
| 180 | +
|
| 181 | + if (elm.tagName === 'BODY') { |
| 182 | + result = window; |
| 183 | + } else if (['scroll', 'auto'].indexOf(getComputedStyle(elm).overflowY) > -1) { |
| 184 | + result = elm; |
| 185 | + } else if (elm.hasAttribute('infinite-wrapper') || elm.hasAttribute('data-infinite-wrapper')) { |
| 186 | + result = elm; |
| 187 | + } |
| 188 | +
|
| 189 | + return result || this.getScrollParent(elm.parentNode); |
| 190 | + }, |
187 | 191 | }, |
188 | 192 | destroyed() { |
189 | 193 | if (!this.isComplete) { |
|
0 commit comments