Skip to content

Commit bbbc8e3

Browse files
committed
height accumulator changes + recycle-list fixes
1 parent ed5234f commit bbbc8e3

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

src/components/RecycleList.vue

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,21 @@
1717
>
1818
<slot
1919
:item="view.item"
20+
:active="view.nr.used"
2021
/>
2122
</div>
2223
</div>
2324

25+
<slot
26+
name="after-container"
27+
/>
28+
2429
<resize-observer @notify="handleResize" />
2530
</div>
2631
</template>
2732

2833
<script>
2934
import Scroller from '../mixins/scroller'
30-
import { clearTimeout, setTimeout } from 'timers';
3135
3236
let uid = 0
3337
@@ -66,10 +70,13 @@ export default {
6670
checkItem: false,
6771
})
6872
},
69-
heights () {
70-
this.updateVisibleItems({
71-
checkItem: false,
72-
})
73+
heights: {
74+
handler () {
75+
this.updateVisibleItems({
76+
checkItem: false,
77+
})
78+
},
79+
deep: true,
7380
},
7481
},
7582
@@ -124,7 +131,7 @@ export default {
124131
unusedPool.push(view)
125132
if (!fake) {
126133
view.nr.used = false
127-
view.top = this.totalHeight
134+
view.top = -9999
128135
this.$_views.delete(view.item)
129136
}
130137
},
@@ -194,10 +201,10 @@ export default {
194201
// Searching for startIndex
195202
do {
196203
oldI = i
197-
h = heights[i]
204+
h = heights[i].accumulator
198205
if (h < scroll.top) {
199206
a = i
200-
} else if (i < count && heights[i + 1] > scroll.top) {
207+
} else if (i < count && heights[i + 1].accumulator > scroll.top) {
201208
b = i
202209
}
203210
i = ~~((a + b) / 2)
@@ -206,10 +213,10 @@ export default {
206213
startIndex = i
207214
208215
// For container style
209-
totalHeight = heights[count - 1]
216+
totalHeight = heights[count - 1].accumulator
210217
211218
// Searching for endIndex
212-
for (endIndex = i; endIndex < count && heights[endIndex] < scroll.bottom; endIndex++);
219+
for (endIndex = i; endIndex < count && heights[endIndex].accumulator < scroll.bottom; endIndex++);
213220
if (endIndex === -1) {
214221
endIndex = items.length - 1
215222
} else {
@@ -275,6 +282,11 @@ export default {
275282
item = items[i]
276283
view = views.get(item)
277284
285+
if (!itemHeight && !heights[i].height) {
286+
if (view) this.unuseView(view)
287+
continue
288+
}
289+
278290
// No view assigned to item
279291
if (!view) {
280292
type = item[typeField]
@@ -315,7 +327,7 @@ export default {
315327
316328
// Update position
317329
if (itemHeight === null) {
318-
view.top = heights[i - 1]
330+
view.top = heights[i - 1].accumulator
319331
} else {
320332
view.top = i * itemHeight
321333
}

src/components/VirtualScroller.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,10 @@ export default {
198198
// Searching for startIndex
199199
do {
200200
oldI = i
201-
h = heights[i]
201+
h = heights[i].accumulator
202202
if (h < scrollTop) {
203203
a = i
204-
} else if (i < l && heights[i + 1] > scrollTop) {
204+
} else if (i < l && heights[i + 1].accumulator > scrollTop) {
205205
b = i
206206
}
207207
i = ~~((a + b) / 2)
@@ -210,11 +210,11 @@ export default {
210210
startIndex = i
211211
212212
// For containers style
213-
offsetTop = i > 0 ? heights[i - 1] : 0
214-
containerHeight = heights[l - 1]
213+
offsetTop = i > 0 ? heights[i - 1].accumulator : 0
214+
containerHeight = heights[l - 1].accumulator
215215
216216
// Searching for endIndex
217-
for (endIndex = i; endIndex < l && heights[endIndex] < scrollBottom; endIndex++);
217+
for (endIndex = i; endIndex < l && heights[endIndex].accumulator < scrollBottom; endIndex++);
218218
if (endIndex === -1) {
219219
endIndex = items.length - 1
220220
} else {

src/mixins/scroller.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export default {
2121
type: [Number, String],
2222
default: null,
2323
},
24+
minItemHeight: {
25+
type: [Number, String],
26+
default: null,
27+
},
2428
heightField: {
2529
type: String,
2630
default: 'height',
@@ -57,14 +61,17 @@ export default {
5761
heights () {
5862
if (this.itemHeight === null) {
5963
const heights = {
60-
'-1': 0,
64+
'-1': { accumulator: 0 },
6165
}
6266
const items = this.items
6367
const field = this.heightField
68+
const minItemHeight = this.minItemHeight
6469
let accumulator = 0
70+
let current
6571
for (let i = 0, l = items.length; i < l; i++) {
66-
accumulator += items[i][field]
67-
heights[i] = accumulator
72+
current = items[i][field] || minItemHeight
73+
accumulator += current
74+
heights[i] = { accumulator, height: current }
6875
}
6976
return heights
7077
}
@@ -132,7 +139,7 @@ export default {
132139
scrollToItem (index) {
133140
let scrollTop
134141
if (this.itemHeight === null) {
135-
scrollTop = index > 0 ? this.heights[index - 1] : 0
142+
scrollTop = index > 0 ? this.heights[index - 1].accumulator : 0
136143
} else {
137144
scrollTop = index * this.itemHeight
138145
}

0 commit comments

Comments
 (0)