Merge branch 'dev' into next

This commit is contained in:
chenjiahan 2020-08-19 16:32:23 +08:00
commit 0075fa3ccd
3 changed files with 82 additions and 61 deletions

View File

@ -20,7 +20,7 @@ ImagePreview([
]); ]);
``` ```
### Custom config ### Set Start Position
```js ```js
ImagePreview({ ImagePreview({
@ -29,9 +29,6 @@ ImagePreview({
'https://img.yzcdn.cn/vant/apple-2.jpg', 'https://img.yzcdn.cn/vant/apple-2.jpg',
], ],
startPosition: 1, startPosition: 1,
onClose() {
// do something
},
}); });
``` ```
@ -49,6 +46,22 @@ ImagePreview({
}); });
``` ```
### Close Event
```js
import { Toast } from 'vant';
ImagePreview({
images: [
'https://img.yzcdn.cn/vant/apple-1.jpg',
'https://img.yzcdn.cn/vant/apple-2.jpg',
],
onClose() {
Toast('closed');
},
});
```
### Async Close ### Async Close
```js ```js
@ -62,7 +75,7 @@ const instance = ImagePreview({
setTimeout(() => { setTimeout(() => {
instance.close(); instance.close();
}, 1000); }, 2000);
``` ```
### Component Call ### Component Call
@ -85,7 +98,6 @@ export default {
], ],
}; };
}, },
methods: { methods: {
onChange(index) { onChange(index) {
this.index = index; this.index = index;

View File

@ -46,9 +46,9 @@ ImagePreview([
]); ]);
``` ```
### 传入配置项 ### 指定初始位置
通过传入配置对象,可以指定初始图片的位置、监听关闭事件 ImagePreview 支持传入配置对象,并通过 `startPosition` 选项指定图片的初始位置(索引值)
```js ```js
ImagePreview({ ImagePreview({
@ -57,9 +57,6 @@ ImagePreview({
'https://img.yzcdn.cn/vant/apple-2.jpg', 'https://img.yzcdn.cn/vant/apple-2.jpg',
], ],
startPosition: 1, startPosition: 1,
onClose() {
// do something
},
}); });
``` ```
@ -77,6 +74,24 @@ ImagePreview({
}); });
``` ```
### 监听关闭事件
通过 `onClose` 选项监听图片预览的关闭事件。
```js
import { Toast } from 'vant';
ImagePreview({
images: [
'https://img.yzcdn.cn/vant/apple-1.jpg',
'https://img.yzcdn.cn/vant/apple-2.jpg',
],
onClose() {
Toast('关闭');
},
});
```
### 异步关闭 ### 异步关闭
通过`asyncClose`属性可以开启异步关闭,开启后异步关闭后,只能通过实例上的 close 方法关闭图片预览。 通过`asyncClose`属性可以开启异步关闭,开启后异步关闭后,只能通过实例上的 close 方法关闭图片预览。
@ -92,7 +107,7 @@ const instance = ImagePreview({
setTimeout(() => { setTimeout(() => {
instance.close(); instance.close();
}, 1000); }, 2000);
``` ```
### 组件调用 ### 组件调用
@ -117,7 +132,6 @@ export default {
], ],
}; };
}, },
methods: { methods: {
onChange(index) { onChange(index) {
this.index = index; this.index = index;

View File

@ -1,33 +1,33 @@
<template> <template>
<demo-section> <demo-section>
<demo-block :title="t('basicUsage')"> <demo-block card :title="t('basicUsage')">
<van-button type="primary" @click="showImagePreview"> <van-cell is-link @click="showImagePreview">
{{ t('button1') }} {{ t('showImages') }}
</van-button> </van-cell>
</demo-block> </demo-block>
<demo-block :title="t('button2')"> <demo-block card :title="t('customConfig')">
<van-button type="primary" @click="showImagePreview(1)"> <van-cell is-link @click="showImagePreview({ startPosition: 1 })">
{{ t('button2') }} {{ t('startPosition') }}
</van-button> </van-cell>
<van-cell is-link @click="showImagePreview({ closeable: true })">
{{ t('showClose') }}
</van-cell>
<van-cell is-link @click="showImagePreview({ onClose })">
{{ t('closeEvent') }}
</van-cell>
</demo-block> </demo-block>
<demo-block :title="t('button4')"> <demo-block card :title="t('asyncClose')">
<van-button type="primary" @click="showImagePreview(0, 0, true)"> <van-cell is-link @click="showImagePreview({ asyncClose: true })">
{{ t('button4') }} {{ t('asyncClose') }}
</van-button> </van-cell>
</demo-block> </demo-block>
<demo-block :title="t('button3')"> <demo-block card :title="t('componentCall')">
<van-button type="primary" @click="showImagePreview(0, 3000)"> <van-cell is-link @click="componentCall">
{{ t('button3') }}
</van-button>
</demo-block>
<demo-block :title="t('componentCall')">
<van-button type="primary" @click="componentCall">
{{ t('componentCall') }} {{ t('componentCall') }}
</van-button> </van-cell>
<van-image-preview v-model="show" :images="images" @change="onChange"> <van-image-preview v-model="show" :images="images" @change="onChange">
<template #index>{{ t('index', index) }}</template> <template #index>{{ t('index', index) }}</template>
</van-image-preview> </van-image-preview>
@ -48,18 +48,24 @@ const images = [
export default { export default {
i18n: { i18n: {
'zh-CN': { 'zh-CN': {
button1: '预览图片', closed: '关闭',
button2: '指定初始位置', showClose: '展示关闭按钮',
button3: '异步关闭', showImages: '预览图片',
button4: '展示关闭按钮', asyncClose: '异步关闭',
closeEvent: '监听关闭事件',
customConfig: '传入配置项',
startPosition: '指定初始位置',
componentCall: '组件调用', componentCall: '组件调用',
index: (index) => `${index + 1}`, index: (index) => `${index + 1}`,
}, },
'en-US': { 'en-US': {
button1: 'Show Images', closed: 'closed',
button2: 'Custom Start Position', showClose: 'Show Close Icon',
button3: 'Async Close', showImages: 'Show Images',
button4: 'Show Close Icon', asyncClose: 'Async Close',
closeEvent: 'Close Event',
customConfig: 'Custom Config',
startPosition: 'Set Start Position',
componentCall: 'Component Call', componentCall: 'Component Call',
index: (index) => `Page: ${index}`, index: (index) => `Page: ${index}`,
}, },
@ -74,6 +80,10 @@ export default {
}, },
methods: { methods: {
onClose() {
this.$toast(this.t('closed'));
},
componentCall() { componentCall() {
this.show = true; this.show = true;
}, },
@ -82,33 +92,18 @@ export default {
this.index = index; this.index = index;
}, },
showImagePreview(position, timer, closeable) { showImagePreview(options) {
const instance = ImagePreview({ const instance = ImagePreview({
images, images,
swipeDuration: 300, ...options,
asyncClose: !!timer,
closeable,
startPosition: typeof position === 'number' ? position : 0,
}); });
if (timer) { if (options.asyncClose) {
setTimeout(() => { setTimeout(() => {
instance.close(); instance.close();
}, timer); }, 2000);
} }
}, },
}, },
}; };
</script> </script>
<style lang="less">
@import '../../style/var';
.demo-image-preview {
background-color: @white;
.van-button {
margin-left: @padding-md;
}
}
</style>