@@ -81,16 +81,20 @@ export default {
81
81
},
82
82
buffer: {
83
83
type: [Number , String ],
84
- default: 2 ,
84
+ default: 200 ,
85
85
},
86
86
poolSize: {
87
87
type: [Number , String ],
88
- default: 1 ,
88
+ default: 2000 ,
89
89
},
90
90
prerender: {
91
91
type: [Number , String ],
92
92
default: 0 ,
93
93
},
94
+ emitUpdate: {
95
+ type: Boolean ,
96
+ default: false ,
97
+ },
94
98
},
95
99
96
100
data : () => ({
@@ -123,16 +127,14 @@ export default {
123
127
},
124
128
125
129
watch: {
126
- items: {
127
- handler () {
128
- this .updateVisibleItems (true )
129
- },
130
- deep: true ,
130
+ heights () {
131
+ this .updateVisibleItems (true )
131
132
},
132
133
pageMode () {
133
134
this .applyPageMode ()
134
135
this .updateVisibleItems (true )
135
136
},
137
+ itemHeight: ' setDirty' ,
136
138
},
137
139
138
140
methods: {
@@ -179,6 +181,18 @@ export default {
179
181
let startIndex = - 1
180
182
let endIndex = - 1
181
183
184
+ const buffer = parseInt (this .buffer )
185
+ const poolSize = parseInt (this .poolSize )
186
+ const scrollTop = ~~ ((scroll .top - buffer) / poolSize) * poolSize
187
+ const scrollBottom = ~~ (Math .ceil ((scroll .bottom + buffer) / poolSize)) * poolSize
188
+
189
+ if (! force && scrollTop === this ._oldScrollTop && scrollBottom === this ._oldScrollBottom ) {
190
+ return
191
+ } else {
192
+ this ._oldScrollTop = scrollTop
193
+ this ._oldScrollBottom = scrollBottom
194
+ }
195
+
182
196
// Variable height mode
183
197
if (itemHeight === null ) {
184
198
const heights = this .heights
@@ -192,60 +206,55 @@ export default {
192
206
do {
193
207
oldI = i
194
208
h = heights[i]
195
- if (h < scroll . top ) {
209
+ if (h < scrollTop ) {
196
210
a = i
197
- } else if (i < l && heights[i + 1 ] > scroll . top ) {
211
+ } else if (i < l && heights[i + 1 ] > scrollTop ) {
198
212
b = i
199
213
}
200
214
i = ~~ ((a + b) / 2 )
201
215
} while (i !== oldI)
216
+ i < 0 && (i = 0 )
202
217
startIndex = i
203
218
204
219
// For containers style
205
220
offsetTop = i > 0 ? heights[i - 1 ] : 0
206
221
containerHeight = heights[l - 1 ]
207
222
208
223
// Searching for endIndex
209
- for (endIndex = i; endIndex < l && heights[endIndex] < scroll . bottom ; endIndex++ );
224
+ for (endIndex = i; endIndex < l && heights[endIndex] < scrollBottom ; endIndex++ );
210
225
if (endIndex === - 1 ) {
211
226
endIndex = items .length - 1
212
227
} else {
213
228
endIndex++
229
+ // Bounds
230
+ endIndex > l && (endIndex = l)
214
231
}
215
232
} else {
216
233
// Fixed height mode
217
- const buffer = this .buffer
218
- const poolSize = this .poolSize
219
- startIndex = ~~ ((~~ (scroll .top / itemHeight) - buffer) / poolSize) * poolSize
220
- endIndex = ~~ ((Math .ceil (scroll .bottom / itemHeight) + buffer) / poolSize) * poolSize
221
- }
234
+ startIndex = ~~ (scrollTop / itemHeight)
235
+ endIndex = Math .ceil (scrollBottom / itemHeight)
222
236
223
- if (startIndex < 0 ) {
224
- startIndex = 0
225
- }
226
- if (endIndex > l) {
227
- endIndex = l
228
- }
237
+ // Bounds
238
+ startIndex < 0 && (startIndex = 0 )
239
+ endIndex > l && (endIndex = l)
229
240
230
- if (itemHeight !== null ) {
231
- // Fixed height mode
232
241
offsetTop = startIndex * itemHeight
233
242
containerHeight = l * itemHeight
234
243
}
235
244
236
- if (force || startIndex !== this ._startIndex || endIndex !== this ._endIndex || l !== this ._length ) {
237
- this .keysEnabled = ! (startIndex > this ._endIndex || endIndex < this ._startIndex )
238
- this ._startIndex = startIndex
239
- this ._endIndex = endIndex
240
- this ._length = l
241
- this .visibleItems = items .slice (startIndex, endIndex)
242
- this .itemContainerStyle = {
243
- height: containerHeight + ' px' ,
244
- }
245
- this .itemsStyle = {
246
- marginTop: offsetTop + ' px' ,
247
- }
245
+ this .keysEnabled = ! (startIndex > this ._endIndex || endIndex < this ._startIndex )
246
+ this ._startIndex = startIndex
247
+ this ._endIndex = endIndex
248
+ this ._length = l
249
+ this .visibleItems = items .slice (startIndex, endIndex)
250
+ this .itemContainerStyle = {
251
+ height: containerHeight + ' px' ,
252
+ }
253
+ this .itemsStyle = {
254
+ marginTop: offsetTop + ' px' ,
248
255
}
256
+
257
+ this .emitUpdate && this .$emit (' update' , startIndex, endIndex)
249
258
}
250
259
},
251
260
@@ -259,6 +268,11 @@ export default {
259
268
this .$el .scrollTop = scrollTop
260
269
},
261
270
271
+ setDirty () {
272
+ this ._oldScrollTop = null
273
+ this ._oldScrollBottom = null
274
+ },
275
+
262
276
applyPageMode () {
263
277
if (this .pageMode ) {
264
278
this .addWindowScroll ()
@@ -297,6 +311,8 @@ export default {
297
311
created () {
298
312
this ._ready = false
299
313
this ._startIndex = 0
314
+ this ._oldScrollTop = null
315
+ this ._oldScrollBottom = null
300
316
const prerender = parseInt (this .prerender )
301
317
if (prerender > 0 ) {
302
318
this .visibleItems = this .items .slice (0 , prerender)
@@ -311,7 +327,7 @@ export default {
311
327
mounted () {
312
328
this .applyPageMode ()
313
329
this .$nextTick (() => {
314
- this .updateVisibleItems ()
330
+ this .updateVisibleItems (true )
315
331
this ._ready = true
316
332
this .$nextTick (this .updateVisibleItems )
317
333
})
0 commit comments