Skip to content

Commit 0f08ac8

Browse files
committed
fix: add or reduce frame count in slider.
1 parent 0409b25 commit 0f08ac8

File tree

1 file changed

+101
-32
lines changed

1 file changed

+101
-32
lines changed

src/components/slider/slideMixin.js

Lines changed: 101 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,32 @@ export default {
4646
}
4747
}
4848
}
49+
50+
const frameCount = this.frameCount
51+
if (!this._preFrameCount) {
52+
this._preFrameCount = frameCount
53+
}
54+
else if (this._preFrameCount !== frameCount) {
55+
this._resetNodes()
56+
this._preFrameCount = frameCount
57+
const resetBlankFrame = () => {
58+
if (this.currentIndex >= frameCount) {
59+
// reset blank page.
60+
this._stopAutoPlay()
61+
this._slideTo(0)
62+
}
63+
}
64+
if (this._sliding) {
65+
// If it's sliding, then the currentIndex is the last frame. The actual currentIndex
66+
// should be the next index.
67+
// That is to say, this updating happens Between _parepareNodes and _rearrangeNodes,
68+
// and the sliding is not yet finished, and the state is not updated yet.
69+
setTimeout(resetBlankFrame, TRANSITION_TIME + NEIGHBOR_SCALE_TIME)
70+
}
71+
else {
72+
resetBlankFrame()
73+
}
74+
}
4975
weex.utils.fireLazyload(this.$el, true)
5076
if (this._preIndex !== this.currentIndex) {
5177
this._slideTo(this.currentIndex)
@@ -156,7 +182,6 @@ export default {
156182
return
157183
}
158184
}
159-
160185
if (!this._preIndex && this._preIndex !== 0) {
161186
if (this._showNodes && this._showNodes[0]) {
162187
this._preIndex = this._showNodes[0].index
@@ -282,23 +307,44 @@ export default {
282307
node._showIndex = 0
283308
return
284309
}
310+
285311
const showCount = this._showCount = Math.abs(step) + 3
286312
this._showStartIdx = step <= 0 ? -1 : 2 - showCount
287313
this._showEndIdx = step <= 0 ? showCount - 2 : 1
288314
this._clearNodesOffset()
289315
this._positionNodes(this._showStartIdx, this._showEndIdx, step)
290316
},
291317

318+
_clearClones () {
319+
// clear all clones.
320+
Object.keys(this._clones).forEach(key => {
321+
this._clones[key].forEach(cloneNode => {
322+
cloneNode.parentNode.removeChild(cloneNode)
323+
})
324+
this._clones[key] = []
325+
})
326+
},
327+
328+
// reset nodes' index and _inShow state. But leave the styles
329+
// as they are to prevent dom rerendering.
330+
_resetNodes () {
331+
this._clearClones()
332+
// reset status.
333+
this._cells.forEach((cell, idx) => {
334+
const elm = cell.elm
335+
elm.index = idx
336+
elm._inShow = false
337+
})
338+
},
339+
292340
_initNodes () {
293-
const total = this.frameCount
294-
const cells = this._cells
295-
for (let i = 0; i < total; i++) {
296-
const node = cells[i].elm
297-
node.index = i
341+
this._cells.forEach((cell, idx) => {
342+
const node = cell.elm
343+
node.index = idx
298344
node._inShow = false
299345
node.style.zIndex = 0
300346
node.style.opacity = 0
301-
}
347+
})
302348
},
303349

304350
_positionNodes (begin, end, step, anim) {
@@ -319,11 +365,11 @@ export default {
319365
*/
320366
_positionNode (node, index) {
321367
const holder = this._showNodes[index]
322-
if (node._inShow && holder !== node) {
323-
if (holder) { this._removeClone(holder) }
368+
if (node._inShow && (holder !== node || holder._showIndex !== index)) {
369+
if (holder && holder._isClone) { this._removeClone(holder) }
324370
node = this._getClone(node.index)
325371
}
326-
else if (node._inShow) {
372+
else if (node._inShow) { // holder === node
327373
return
328374
}
329375

@@ -339,30 +385,49 @@ export default {
339385
},
340386

341387
_getClone (index) {
342-
let arr = this._clones[index]
343-
if (!arr) {
344-
this._clones[index] = arr = []
345-
}
346-
if (arr.length <= 0) {
347-
const origNode = this._cells[index].elm
348-
const clone = origNode.cloneNode(true)
349-
clone._isClone = true
350-
clone._inShow = origNode._inShow
351-
clone.index = origNode.index
352-
clone.style.opacity = 0
353-
clone.style.zIndex = 0
354-
const ct = this.$refs.inner
355-
ct.appendChild(clone)
356-
arr.push(clone)
357-
}
358-
return arr.pop()
388+
const arr = this._clones[index] || (this._clones[index] = [])
389+
const origNode = this._cells[index].elm
390+
const clone = origNode.cloneNode(true)
391+
clone._isClone = true
392+
clone._inShow = true
393+
// clone._inShow = origNode._inShow
394+
clone.index = origNode.index
395+
clone.style.opacity = 0
396+
clone.style.zIndex = 0
397+
this.$refs.inner.appendChild(clone)
398+
arr.push(clone)
399+
return clone
400+
// try {
401+
// let arr = this._clones[index]
402+
// if (!arr) {
403+
// this._clones[index] = arr = []
404+
// }
405+
// if (arr.length <= 0) {
406+
407+
// }
408+
// return arr.pop()
409+
// } catch (err) {
410+
// console.error('this._cells -> ', this._cells)
411+
// }
359412
},
360413

361414
_removeClone (node) {
362-
const idx = node.index
363-
this._hideNode(node)
364-
const arr = this._clones[idx]
365-
arr.push(node)
415+
const cloneArr = this._clones[node.index]
416+
let i
417+
if (cloneArr && (i = cloneArr.indexOf(node)) > -1) {
418+
cloneArr.splice(i, 1)
419+
}
420+
try {
421+
node.parentNode.removeChild(node)
422+
}
423+
catch (err) {
424+
// maybe cells has been updated and this clone node is already removed from the dom tree
425+
// throught _clearClones method.
426+
}
427+
// const idx = node.index
428+
// this._hideNode(node)
429+
// const arr = this._clones[idx]
430+
// arr.push(node)
366431
},
367432

368433
_hideNode (node) {
@@ -410,7 +475,11 @@ export default {
410475
* replace a clone node with the original node if it's not in use.
411476
*/
412477
_replaceClone (clone, pos) {
413-
const origNode = this._cells[clone.index].elm
478+
const origCell = this._cells[clone.index]
479+
if (!origCell) {
480+
return
481+
}
482+
const origNode = origCell.elm
414483
if (origNode._inShow) {
415484
return
416485
}

0 commit comments

Comments
 (0)