Skip to content

Commit 60dbe3b

Browse files
committed
Fixed size change case scroll offset error.
1 parent 42371a4 commit 60dbe3b

File tree

1 file changed

+21
-30
lines changed

1 file changed

+21
-30
lines changed

index.js

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,23 @@
5858
start: start, // start index.
5959
end: start + keeps, // end index.
6060
keeps: keeps, // nums keeping in real dom.
61-
total: 0, // all items count, update in render filter.
62-
offset: 0, // cache current scroll offset.
63-
offsetAll: 0, // cache all the scroll offset.
64-
direct: 'd', // cache scroll direction.
61+
total: 0, // all items count, update in filter.
62+
offsetAll: 0, // cache all the scrollable offset.
6563
paddingTop: 0, // container wrapper real padding-top.
6664
paddingBottom: 0, // container wrapper real padding-bottom.
67-
varCache: {}, // cache variable index height and padding offset.
65+
varCache: {}, // object to cache variable index height and scroll offset.
6866
varAverSize: 0, // average/estimate item height before variable be calculated.
6967
varLastCalcIndex: 0 // last calculated variable height/offset index, always increase.
7068
}
7169
},
7270

7371
watch: {
74-
remain: function () {
75-
this.alter = 'remain'
76-
},
7772
size: function () {
7873
this.alter = 'size'
7974
},
75+
remain: function () {
76+
this.alter = 'remain'
77+
},
8078
bench: function () {
8179
this.alter = 'bench'
8280
},
@@ -90,9 +88,6 @@
9088
var delta = this.delta
9189
var offset = this.$refs.vsl.scrollTop
9290

93-
delta.direct = delta.offset > offset ? 'u' : 'd'
94-
delta.offset = offset
95-
9691
if (!offset && delta.total) {
9792
this.triggerEvent('totop')
9893
}
@@ -106,15 +101,11 @@
106101
}
107102

108103
if (this.onscroll) {
109-
this.onscroll(e, {
110-
end: delta.end,
111-
start: delta.start,
112-
offset: offset
113-
})
104+
this.onscroll(e, offset)
114105
}
115106
},
116107

117-
// update render zone by moving offset.
108+
// update render zone by scroll offset.
118109
updateZone: function (offset) {
119110
var overs
120111
if (this.variable) {
@@ -137,7 +128,7 @@
137128
this.$forceUpdate()
138129
},
139130

140-
// return the scroll passed items count in variable height.
131+
// return the scroll passed items count in variable.
141132
getVarOvers: function (offset) {
142133
var low = 0
143134
var middle = 0
@@ -149,7 +140,7 @@
149140
middle = low + Math.floor((high - low) / 2)
150141
middleOffset = this.getVarOffset(middle)
151142

152-
// calculate the average variable size at first binary search.
143+
// calculate the average variable height at first binary search.
153144
if (!delta.varAverSize) {
154145
delta.varAverSize = Math.floor(middleOffset / middle)
155146
}
@@ -166,7 +157,7 @@
166157
return low > 0 ? --low : 0
167158
},
168159

169-
// get the variable height index scroll offset.
160+
// return a variable scroll offset from given index.
170161
getVarOffset: function (index, nocache) {
171162
var delta = this.delta
172163
var cache = delta.varCache[index]
@@ -191,7 +182,7 @@
191182
return offset
192183
},
193184

194-
// return a variable size (height) from a given index.
185+
// return a variable size (height) from given index.
195186
getVarSize: function (index, nocache) {
196187
var cache = this.delta.varCache[index]
197188
return (!nocache && cache && cache.size) || this.variable(index) || 0
@@ -217,7 +208,7 @@
217208
}
218209
},
219210

220-
// retun the variable all heights use to judge reach to bottom.
211+
// retun the variable all heights use to judge reach bottom.
221212
getVarAllHeight: function () {
222213
var delta = this.delta
223214
if (delta.total - delta.end <= delta.keeps || delta.varLastCalcIndex === delta.total - 1) {
@@ -227,9 +218,9 @@
227218
}
228219
},
229220

230-
// the ONLY ONE public method, let the parent to update variable by index.
221+
// the ONLY ONE public method, allow the parent update variable by index.
231222
updateVariable: function (index) {
232-
// update all the offfsets ahead of index.
223+
// clear/update all the offfsets and heights ahead of index.
233224
this.getVarOffset(index, true)
234225
},
235226

@@ -265,7 +256,7 @@
265256
}
266257
},
267258

268-
// set manual scrollTop.
259+
// set manual scroll top.
269260
setScrollTop: function (scrollTop) {
270261
this.$refs.vsl.scrollTop = scrollTop
271262
},
@@ -318,19 +309,19 @@
318309
delta.keeps = this.remain + (this.bench || this.remain)
319310

320311
var alterStart = this.alter === 'start'
321-
var oldStart = alterStart ? this.start : delta.start
322-
var zone = this.getZone(oldStart)
312+
var calcStart = alterStart ? this.start : delta.start
313+
var zone = this.getZone(calcStart)
323314

324-
// if start change, update scroll position.
325-
if (alterStart) {
315+
// if start or size change, update scroll position.
316+
if (alterStart || this.alter === 'size') {
326317
this.$nextTick(this.setScrollTop.bind(this, this.variable
327318
? this.getVarOffset(zone.isLast ? delta.total : zone.start)
328319
: zone.isLast ? delta.total * this.size : zone.start * this.size)
329320
)
330321
}
331322

332323
// if points out difference, force update once again.
333-
if (oldStart !== zone.start || this.alter) {
324+
if (calcStart !== zone.start || this.alter) {
334325
this.alter = ''
335326
delta.end = zone.end
336327
delta.start = zone.start

0 commit comments

Comments
 (0)