From cbe136ebdde5bae84ac13760c664e4b0e18bd743 Mon Sep 17 00:00:00 2001 From: Fyerl Date: Wed, 1 Apr 2020 18:02:15 +0800 Subject: [PATCH] =?UTF-8?q?feat(Swipe):=20=E4=BC=98=E5=8C=96=20loop:=20fal?= =?UTF-8?q?se=20=E5=9C=BA=E6=99=AF=E4=B8=8B=E7=9A=84=E7=BF=BB=E9=A1=B5?= =?UTF-8?q?=E9=80=BB=E8=BE=91=20(#5953)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/swipe/index.js | 21 ++++++++++++++++++--- src/swipe/test/index.spec.js | 17 ----------------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/swipe/index.js b/src/swipe/index.js index 94b41be4a..f8d67f83c 100644 --- a/src/swipe/index.js +++ b/src/swipe/index.js @@ -97,6 +97,10 @@ export default createComponent({ return this.children.length; }, + maxCount() { + return Math.ceil(Math.abs(this.minOffset) / this.size); + }, + delta() { return this.vertical ? this.deltaY : this.deltaX; }, @@ -215,8 +219,19 @@ export default createComponent({ if (this.delta && this.isCorrectDirection) { const offset = this.vertical ? this.offsetY : this.offsetX; + + let pace = 0; + + if (this.loop) { + pace = offset > 0 ? (this.delta > 0 ? -1 : 1) : 0; + } else { + pace = -Math[this.delta > 0 ? 'ceil' : 'floor']( + this.delta / this.size + ); + } + this.move({ - pace: offset > 0 ? (this.delta > 0 ? -1 : 1) : 0, + pace, emitChange: true, }); } @@ -226,14 +241,14 @@ export default createComponent({ }, getTargetActive(pace) { - const { active, count } = this; + const { active, count, maxCount } = this; if (pace) { if (this.loop) { return range(active + pace, -1, count); } - return range(active + pace, 0, count - 1); + return range(active + pace, 0, maxCount); } return active; diff --git a/src/swipe/test/index.spec.js b/src/swipe/test/index.spec.js index acb40688f..ee5b1cb10 100644 --- a/src/swipe/test/index.spec.js +++ b/src/swipe/test/index.spec.js @@ -137,23 +137,6 @@ test('loop', () => { expect(swipe.active).toEqual(1); }); -test('not loop', () => { - const wrapper = mount(Component, { - propsData: { - loop: false, - }, - }); - const { swipe } = wrapper.vm.$refs; - const track = wrapper.find('.van-swipe__track'); - - triggerDrag(track, -100, 0); - expect(swipe.active).toEqual(1); - triggerDrag(track, -100, 0); - expect(swipe.active).toEqual(2); - triggerDrag(track, -100, 0); - expect(swipe.active).toEqual(2); -}); - test('should pause auto play when page hidden', async () => { const change = jest.fn(); mount(Component, {