feat(Swipe): pause autoplay when page hidden

This commit is contained in:
陈嘉涵 2020-01-16 10:56:45 +08:00
parent 60173dd6bc
commit 11315787ec
2 changed files with 40 additions and 2 deletions

View File

@ -12,6 +12,7 @@ export default createComponent({
TouchMixin, TouchMixin,
BindEventMixin(function(bind, isBind) { BindEventMixin(function(bind, isBind) {
bind(window, 'resize', this.resize, true); bind(window, 'resize', this.resize, true);
bind(window, 'visibilitychange', this.onVisibilityChange);
if (isBind) { if (isBind) {
this.initialize(); this.initialize();
@ -165,6 +166,14 @@ export default createComponent({
this.initialize(this.activeIndicator); this.initialize(this.activeIndicator);
}, },
onVisibilityChange() {
if (document.hidden) {
this.clear();
} else {
this.autoPlay();
}
},
onTouchStart(event) { onTouchStart(event) {
if (!this.touchable) return; if (!this.touchable) return;

View File

@ -1,8 +1,19 @@
import { mount, triggerDrag, later } from '../../../test'; import { mount, triggerDrag, later, trigger } from '../../../test';
function mockPageHidden() {
let hidden = true;
Object.defineProperty(document, 'hidden', {
get: () => hidden
});
trigger(window, 'visibilitychange');
hidden = false;
}
const Component = { const Component = {
template: ` template: `
<van-swipe ref="swipe" v-bind="$props"> <van-swipe ref="swipe" v-bind="$props" v-on="$listeners">
<van-swipe-item :style="style">1</van-swipe-item> <van-swipe-item :style="style">1</van-swipe-item>
<van-swipe-item :style="style">2</van-swipe-item> <van-swipe-item :style="style">2</van-swipe-item>
<van-swipe-item :style="style">3</van-swipe-item> <van-swipe-item :style="style">3</van-swipe-item>
@ -142,3 +153,21 @@ test('not loop', () => {
triggerDrag(track, -100, 0); triggerDrag(track, -100, 0);
expect(swipe.active).toEqual(2); expect(swipe.active).toEqual(2);
}); });
test('should pause auto play when page hidden', async () => {
const change = jest.fn();
mount(Component, {
propsData: {
loop: true,
autoplay: 1
},
listeners: {
change
}
});
mockPageHidden();
await later(50);
expect(change).toHaveBeenCalledTimes(0);
});