Skip to content

Commit fb12d46

Browse files
Build source
1 parent 6b74e1f commit fb12d46

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

dist/index.js

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterat
7171
type: Boolean,
7272
default: false
7373
},
74+
scrollelement: {
75+
type: HTMLElement,
76+
default: null
77+
},
7478
start: {
7579
type: Number,
7680
default: 0
@@ -145,6 +149,11 @@ function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterat
145149
itemcount: function itemcount() {
146150
this.changeProp = 'itemcount';
147151
this.itemModeForceRender();
152+
},
153+
scrollelement: function scrollelement(newScrollelement, oldScrollelement) {
154+
if (this.pagemode) return;
155+
if (oldScrollelement) this.removeScrollListener(oldScrollelement);
156+
if (newScrollelement) this.addScrollListener(newScrollelement);
148157
}
149158
},
150159
created: function created() {
@@ -179,7 +188,9 @@ function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterat
179188
},
180189
mounted: function mounted() {
181190
if (this.pagemode) {
182-
window.addEventListener('scroll', this.debounce ? _debounce(this.onScroll.bind(this), this.debounce) : this.onScroll, false);
191+
this.addScrollListener(window);
192+
} else if (this.scrollelement) {
193+
this.addScrollListener(this.scrollelement);
183194
}
184195

185196
if (this.start) {
@@ -191,7 +202,9 @@ function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterat
191202
},
192203
beforeDestroy: function beforeDestroy() {
193204
if (this.pagemode) {
194-
window.removeEventListener('scroll', this.debounce ? _debounce(this.onScroll.bind(this), this.debounce) : this.onScroll, false);
205+
this.removeScrollListener(window);
206+
} else if (this.scrollelement) {
207+
this.removeScrollListener(this.scrollelement);
195208
}
196209
},
197210
// check if delta should update when props change.
@@ -215,10 +228,33 @@ function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterat
215228
}
216229
},
217230
methods: {
231+
// add pagemode/scrollelement scroll event listener
232+
addScrollListener: function addScrollListener(element) {
233+
this.scrollHandler = this.debounce ? _debounce(this.onScroll.bind(this), this.debounce) : this.onScroll;
234+
element.addEventListener('scroll', this.scrollHandler, false);
235+
},
236+
// remove pagemode/scrollelement scroll event listener
237+
removeScrollListener: function removeScrollListener(element) {
238+
element.removeEventListener('scroll', this.scrollHandler, false);
239+
},
218240
onScroll: function onScroll(event) {
219241
var delta = this.delta;
220242
var vsl = this.$refs.vsl;
221-
var offset = this.pagemode ? window.pageYOffset : (vsl.$el || vsl).scrollTop || 0;
243+
var offset;
244+
245+
if (this.pagemode) {
246+
var elemRect = this.$el.getBoundingClientRect();
247+
offset = -elemRect.top;
248+
} else if (this.scrollelement) {
249+
var scrollelementRect = this.scrollelement.getBoundingClientRect();
250+
251+
var _elemRect = this.$el.getBoundingClientRect();
252+
253+
offset = scrollelementRect.top - _elemRect.top;
254+
} else {
255+
offset = (vsl.$el || vsl).scrollTop || 0;
256+
}
257+
222258
delta.direction = offset > delta.scrollTop ? 'D' : 'U';
223259
delta.scrollTop = offset;
224260

@@ -431,6 +467,8 @@ function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterat
431467
setScrollTop: function setScrollTop(scrollTop) {
432468
if (this.pagemode) {
433469
window.scrollTo(0, scrollTop);
470+
} else if (this.scrollelement) {
471+
this.scrollelement.scrollTo(0, scrollTop);
434472
} else {
435473
var vsl = this.$refs.vsl;
436474

@@ -475,14 +513,6 @@ function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterat
475513
paddingBottom = 0;
476514
}
477515

478-
if (this.pagemode && this.$el && this.$el.parentElement) {
479-
var bodyRect = document.body.getBoundingClientRect();
480-
var elemRect = this.$el.parentElement.getBoundingClientRect();
481-
var offset = elemRect.top - bodyRect.top;
482-
paddingTop -= offset;
483-
if (paddingTop < 0) paddingTop = 0;
484-
}
485-
486516
delta.paddingTop = paddingTop;
487517
delta.paddingBottom = paddingBottom;
488518
delta.offsetAll = allHeight - this.size * this.remain;
@@ -521,7 +551,7 @@ function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterat
521551
}
522552
}, list); // page mode just render list, no wraper.
523553

524-
if (this.pagemode) {
554+
if (this.pagemode || this.scrollelement) {
525555
return renderList;
526556
}
527557

0 commit comments

Comments
 (0)