Skip to content

Commit 74f8de5

Browse files
committed
Add updateVariable method.
1 parent a09b520 commit 74f8de5

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

index.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@
110110
this.triggerEvent('totop')
111111
}
112112

113-
this.updateZone(offset)
113+
if (delta.total > delta.keeps) {
114+
this.updateZone(offset)
115+
}
114116

115117
if (this.onscroll) {
116118
this.onscroll(e, {
@@ -173,34 +175,34 @@
173175
},
174176

175177
// get the variable height index scroll offset.
176-
getVarOffset: function (index) {
178+
getVarOffset: function (index, nocache) {
177179
var delta = this.delta
178180
var cache = delta.varCache[index]
179181

180-
if (cache) {
182+
if (cache && !nocache) {
181183
return cache.offset
182184
}
183185

184186
var offset = 0
185187
for (var i = 0; i < index; i++) {
186-
var size = this.getVarSize(i)
188+
var size = this.getVarSize(i, nocache)
187189
delta.varCache[i] = {
188190
size: size,
189191
offset: offset
190192
}
191193
offset += size
192194
}
193195

194-
delta.varLastCalcIndex = Math.max(delta.varLastCalcIndex, index)
196+
delta.varLastCalcIndex = Math.max(delta.varLastCalcIndex, index - 1)
195197
delta.varLastCalcIndex = Math.min(delta.varLastCalcIndex, delta.total - 1)
196198

197199
return offset
198200
},
199201

200202
// return a variable size (height) from a given index.
201-
getVarSize: function (index) {
203+
getVarSize: function (index, nocache) {
202204
var cache = this.delta.varCache[index]
203-
return (cache && cache.size) || this.variable(index) || 0
205+
return (!nocache && cache && cache.size) || this.variable(index) || 0
204206
},
205207

206208
// return the variable paddingTop base current zone.
@@ -223,10 +225,16 @@
223225
}
224226
},
225227

228+
// the ONLY ONE public method, let the parent to update variable by index.
229+
updateVariable: function (index) {
230+
// update all the offfsets ahead of index.
231+
this.getVarOffset(index, true)
232+
},
233+
226234
// avoid overflow range.
227235
isOverflow: function (start) {
228236
var delta = this.delta
229-
var overflow = (delta.total - delta.keeps > 0) && (start + this.remain >= delta.total)
237+
var overflow = (delta.total > delta.keeps) && (start + this.remain >= delta.total)
230238
if (overflow && delta.direct === 'd') {
231239
this.triggerEvent('tobottom')
232240
}
@@ -248,8 +256,8 @@
248256
var start, end
249257
var delta = this.delta
250258

251-
index = parseInt(index, 10) || 0
252-
index = index >= delta.total ? (delta.total - 1) : (index < 0 ? 0 : index)
259+
index = parseInt(index, 10)
260+
index = index < 0 ? 0 : index
253261

254262
var overflow = this.isOverflow(index)
255263
// if overflow range return the last zone.

0 commit comments

Comments
 (0)