feat(Swipe): 优化 loop: false 场景下的翻页逻辑 (#5953)

This commit is contained in:
Fyerl 2020-04-01 18:02:15 +08:00 committed by GitHub
parent afc3164364
commit cbe136ebdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 20 deletions

View File

@ -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;

View File

@ -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, {