Skip to content

Commit 42371a4

Browse files
committed
Change event fire way.
1 parent b7f658d commit 42371a4

File tree

1 file changed

+55
-48
lines changed

1 file changed

+55
-48
lines changed

index.js

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@
5959
end: start + keeps, // end index.
6060
keeps: keeps, // nums keeping in real dom.
6161
total: 0, // all items count, update in render filter.
62-
offset: 0, // cache scrollTop offset.
62+
offset: 0, // cache current scroll offset.
63+
offsetAll: 0, // cache all the scroll offset.
6364
direct: 'd', // cache scroll direction.
64-
fireTime: 0, // cache last event fire time avoid compact.
6565
paddingTop: 0, // container wrapper real padding-top.
6666
paddingBottom: 0, // container wrapper real padding-bottom.
6767
varCache: {}, // cache variable index height and padding offset.
@@ -70,22 +70,18 @@
7070
}
7171
},
7272

73-
mounted: function () {
74-
if (this.start) {
75-
var start = this.getZone(this.start).start
76-
this.setScrollTop(this.variable ? this.getVarOffset(start) : start * this.size)
77-
}
78-
},
79-
8073
watch: {
81-
start: function () {
82-
this.alter = 'start'
83-
},
8474
remain: function () {
8575
this.alter = 'remain'
8676
},
77+
size: function () {
78+
this.alter = 'size'
79+
},
8780
bench: function () {
8881
this.alter = 'bench'
82+
},
83+
start: function () {
84+
this.alter = 'start'
8985
}
9086
},
9187

@@ -105,6 +101,10 @@
105101
this.updateZone(offset)
106102
}
107103

104+
if (offset >= delta.offsetAll) {
105+
this.triggerEvent('tobottom')
106+
}
107+
108108
if (this.onscroll) {
109109
this.onscroll(e, {
110110
end: delta.end,
@@ -128,7 +128,7 @@
128128
var bench = this.bench || this.remain
129129

130130
// for better performance, if scroll pass items within now bench, do not update.
131-
if (!zone.overflow && (overs > delta.start) && (overs - delta.start <= bench)) {
131+
if (!zone.isLast && (overs > delta.start) && (overs - delta.start <= bench)) {
132132
return
133133
}
134134

@@ -139,17 +139,17 @@
139139

140140
// return the scroll passed items count in variable height.
141141
getVarOvers: function (offset) {
142-
var delta = this.delta
143142
var low = 0
144143
var middle = 0
145144
var middleOffset = 0
145+
var delta = this.delta
146146
var high = delta.total
147147

148148
while (low <= high) {
149149
middle = low + Math.floor((high - low) / 2)
150150
middleOffset = this.getVarOffset(middle)
151151

152-
// calculate the variable average size at first binary search.
152+
// calculate the average variable size at first binary search.
153153
if (!delta.varAverSize) {
154154
delta.varAverSize = Math.floor(middleOffset / middle)
155155
}
@@ -171,7 +171,7 @@
171171
var delta = this.delta
172172
var cache = delta.varCache[index]
173173

174-
if (cache && !nocache) {
174+
if (!nocache && cache) {
175175
return cache.offset
176176
}
177177

@@ -217,30 +217,20 @@
217217
}
218218
},
219219

220-
// the ONLY ONE public method, let the parent to update variable by index.
221-
updateVariable: function (index) {
222-
// update all the offfsets ahead of index.
223-
this.getVarOffset(index, true)
224-
},
225-
226-
// avoid overflow range.
227-
isOverflow: function (start) {
220+
// retun the variable all heights use to judge reach to bottom.
221+
getVarAllHeight: function () {
228222
var delta = this.delta
229-
var overflow = (delta.total > delta.keeps && start + this.remain >= delta.total) || (start >= delta.total)
230-
if (overflow && delta.direct === 'd') {
231-
this.triggerEvent('tobottom')
223+
if (delta.total - delta.end <= delta.keeps || delta.varLastCalcIndex === delta.total - 1) {
224+
return this.getVarOffset(delta.total)
225+
} else {
226+
return this.getVarOffset(delta.start) + (delta.total - delta.end) * (delta.varAverSize || this.size)
232227
}
233-
return overflow
234228
},
235229

236-
// trigger a props event on parent.
237-
triggerEvent: function (event) {
238-
var now = +new Date()
239-
var delta = this.delta
240-
if (this[event] && now - delta.fireTime > 30) {
241-
this[event]()
242-
delta.fireTime = now
243-
}
230+
// the ONLY ONE public method, let the parent to update variable by index.
231+
updateVariable: function (index) {
232+
// update all the offfsets ahead of index.
233+
this.getVarOffset(index, true)
244234
},
245235

246236
// return the right zone info base on `start/index`.
@@ -251,11 +241,11 @@
251241
index = parseInt(index, 10)
252242
index = index < 0 ? 0 : index
253243

254-
var overflow = this.isOverflow(index)
255-
// if overflow range return the last zone.
256-
if (overflow) {
244+
var lastStart = delta.total - delta.keeps
245+
var isLast = (index <= delta.total && index >= lastStart) || (index > delta.total)
246+
if (isLast) {
257247
end = delta.total
258-
start = Math.max(0, delta.total - delta.keeps)
248+
start = Math.max(0, lastStart)
259249
} else {
260250
start = index
261251
end = start + delta.keeps
@@ -264,7 +254,14 @@
264254
return {
265255
end: end,
266256
start: start,
267-
overflow: overflow
257+
isLast: isLast
258+
}
259+
},
260+
261+
// trigger a props event on parent.
262+
triggerEvent: function (event) {
263+
if (this[event]) {
264+
this[event]()
268265
}
269266
},
270267

@@ -285,27 +282,37 @@
285282

286283
delta.total = slots.length
287284

288-
var paddingTop, paddingBottom
289-
var hasPadding = slots.length > delta.keeps
285+
var paddingTop, paddingBottom, allHeight
286+
var hasPadding = delta.total > delta.keeps
290287

291288
if (this.variable) {
289+
allHeight = this.getVarAllHeight()
292290
paddingTop = hasPadding ? this.getVarPaddingTop() : 0
293291
paddingBottom = hasPadding ? this.getVarPaddingBottom() : 0
294292
} else {
293+
allHeight = this.size * delta.total
295294
paddingTop = this.size * (hasPadding ? delta.start : 0)
296-
paddingBottom = this.size * (hasPadding ? slots.length - delta.keeps : 0) - paddingTop
295+
paddingBottom = this.size * (hasPadding ? delta.total - delta.keeps : 0) - paddingTop
297296
}
298297

299298
delta.paddingTop = paddingTop
300299
delta.paddingBottom = paddingBottom
300+
delta.offsetAll = allHeight - this.size * this.remain
301301

302302
return slots.filter(function (slot, index) {
303303
return index >= delta.start && index <= delta.end
304304
})
305305
}
306306
},
307307

308-
// update delta and zone when prorps change.
308+
mounted: function () {
309+
if (this.start) {
310+
var start = this.getZone(this.start).start
311+
this.setScrollTop(this.variable ? this.getVarOffset(start) : start * this.size)
312+
}
313+
},
314+
315+
// check if delta should update when prorps change.
309316
beforeUpdate: function () {
310317
var delta = this.delta
311318
delta.keeps = this.remain + (this.bench || this.remain)
@@ -314,11 +321,11 @@
314321
var oldStart = alterStart ? this.start : delta.start
315322
var zone = this.getZone(oldStart)
316323

317-
// if changing start, update scroll position after update.
324+
// if start change, update scroll position.
318325
if (alterStart) {
319326
this.$nextTick(this.setScrollTop.bind(this, this.variable
320-
? this.getVarOffset(zone.overflow ? delta.total : zone.start)
321-
: zone.overflow ? delta.total * this.size : zone.start * this.size)
327+
? this.getVarOffset(zone.isLast ? delta.total : zone.start)
328+
: zone.isLast ? delta.total * this.size : zone.start * this.size)
322329
)
323330
}
324331

0 commit comments

Comments
 (0)