Skip to content

Commit 3ec05a4

Browse files
committed
RecycleList new wrapper ref + fixes
1 parent 256d365 commit 3ec05a4

File tree

1 file changed

+44
-39
lines changed

1 file changed

+44
-39
lines changed

src/components/RecycleList.vue

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
v-observe-visibility="handleVisibilityChange"
77
>
88
<div
9+
ref="wrapper"
910
class="item-wrapper"
1011
:style="{ height: totalHeight + 'px' }"
1112
>
@@ -190,50 +191,54 @@ export default {
190191
let startIndex, endIndex
191192
let totalHeight
192193
193-
// Variable height mode
194-
if (itemHeight === null) {
195-
let h
196-
let a = 0
197-
let b = count - 1
198-
let i = ~~(count / 2)
199-
let oldI
200-
201-
// Searching for startIndex
202-
do {
203-
oldI = i
204-
h = heights[i].accumulator
205-
if (h < scroll.top) {
206-
a = i
207-
} else if (i < count - 1 && heights[i + 1].accumulator > scroll.top) {
208-
b = i
194+
if (!count) {
195+
startIndex = endIndex = totalHeight = 0
196+
} else {
197+
// Variable height mode
198+
if (itemHeight === null) {
199+
let h
200+
let a = 0
201+
let b = count - 1
202+
let i = ~~(count / 2)
203+
let oldI
204+
205+
// Searching for startIndex
206+
do {
207+
oldI = i
208+
h = heights[i].accumulator
209+
if (h < scroll.top) {
210+
a = i
211+
} else if (i < count - 1 && heights[i + 1].accumulator > scroll.top) {
212+
b = i
213+
}
214+
i = ~~((a + b) / 2)
215+
} while (i !== oldI)
216+
i < 0 && (i = 0)
217+
startIndex = i
218+
219+
// For container style
220+
totalHeight = heights[count - 1].accumulator
221+
222+
// Searching for endIndex
223+
for (endIndex = i; endIndex < count && heights[endIndex].accumulator < scroll.bottom; endIndex++);
224+
if (endIndex === -1) {
225+
endIndex = items.length - 1
226+
} else {
227+
endIndex++
228+
// Bounds
229+
endIndex > count && (endIndex = count)
209230
}
210-
i = ~~((a + b) / 2)
211-
} while (i !== oldI)
212-
i < 0 && (i = 0)
213-
startIndex = i
214-
215-
// For container style
216-
totalHeight = heights[count - 1].accumulator
217-
218-
// Searching for endIndex
219-
for (endIndex = i; endIndex < count && heights[endIndex].accumulator < scroll.bottom; endIndex++);
220-
if (endIndex === -1) {
221-
endIndex = items.length - 1
222231
} else {
223-
endIndex++
232+
// Fixed height mode
233+
startIndex = ~~(scroll.top / itemHeight)
234+
endIndex = Math.ceil(scroll.bottom / itemHeight)
235+
224236
// Bounds
237+
startIndex < 0 && (startIndex = 0)
225238
endIndex > count && (endIndex = count)
226-
}
227-
} else {
228-
// Fixed height mode
229-
startIndex = ~~(scroll.top / itemHeight)
230-
endIndex = Math.ceil(scroll.bottom / itemHeight)
231239
232-
// Bounds
233-
startIndex < 0 && (startIndex = 0)
234-
endIndex > count && (endIndex = count)
235-
236-
totalHeight = count * itemHeight
240+
totalHeight = count * itemHeight
241+
}
237242
}
238243
239244
this.totalHeight = totalHeight

0 commit comments

Comments
 (0)