index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <template>
  2. <demo-section>
  3. <demo-block :title="$t('basicUsage')">
  4. <van-button @click="showImagePreview">{{ $t('button1') }}</van-button>
  5. </demo-block>
  6. <demo-block :title="$t('button2')">
  7. <van-button @click="showImagePreview(1)">{{ $t('button2') }}</van-button>
  8. </demo-block>
  9. <demo-block :title="$t('button3')">
  10. <van-button @click="showImagePreview(0, 1000)">{{ $t('button3') }}</van-button>
  11. </demo-block>
  12. <demo-block :title="$t('componentCall')">
  13. <van-button @click="componentCall">{{ $t('componentCall') }}</van-button>
  14. <van-image-preview
  15. v-model="show"
  16. :images="images"
  17. @change="onChange"
  18. >
  19. <template v-slot:index>{{ $t('index', index) }}</template>
  20. </van-image-preview>
  21. </demo-block>
  22. </demo-section>
  23. </template>
  24. <script>
  25. import { ImagePreview } from '../..';
  26. const images = [
  27. 'https://img.yzcdn.cn/public_files/2017/09/05/3bd347e44233a868c99cf0fe560232be.jpg',
  28. 'https://img.yzcdn.cn/public_files/2017/09/05/c0dab461920687911536621b345a0bc9.jpg',
  29. 'https://img.yzcdn.cn/public_files/2017/09/05/4e3ea0898b1c2c416eec8c11c5360833.jpg',
  30. 'https://img.yzcdn.cn/public_files/2017/09/05/fd08f07665ed67d50e11b32a21ce0682.jpg'
  31. ];
  32. export default {
  33. i18n: {
  34. 'zh-CN': {
  35. button1: '预览图片',
  36. button2: '指定初始位置',
  37. button3: '异步关闭',
  38. componentCall: '组件调用',
  39. index: index => `第${index + 1}页`
  40. },
  41. 'en-US': {
  42. button1: 'Show Images',
  43. button2: 'Custom Start Position',
  44. button3: 'Async Close',
  45. componentCall: 'Component Call',
  46. index: index => `Page: ${index}`
  47. }
  48. },
  49. data() {
  50. return {
  51. show: false,
  52. images,
  53. index: 0
  54. };
  55. },
  56. methods: {
  57. componentCall() {
  58. this.show = true;
  59. },
  60. onChange(index) {
  61. this.index = index;
  62. },
  63. showImagePreview(position, timer) {
  64. const instance = ImagePreview({
  65. images,
  66. asyncClose: !!timer,
  67. startPosition: typeof position === 'number' ? position : 0
  68. });
  69. if (timer) {
  70. setTimeout(() => {
  71. instance.close();
  72. }, timer);
  73. }
  74. }
  75. }
  76. };
  77. </script>
  78. <style lang="less">
  79. .demo-image-preview {
  80. .van-button {
  81. margin-left: 15px;
  82. }
  83. }
  84. .van-image-preview {
  85. img {
  86. -webkit-user-drag: none;
  87. }
  88. }
  89. </style>