wk-preview-img.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <template>
  2. <view v-if="show" class="previewImage" :style="{ opacity: opacity}" @tap="close" @touchmove.stop.prevent>
  3. <swiper class="swiper" :current="index" @change="change">
  4. <swiper-item v-for="(img, i) in imgs" :key="i">
  5. <movable-area class="marea" scale-area>
  6. <movable-view class="mview" direction="all" scale="true" scale-min="0.5" scale-max="4" :scale-value="scale" @scale="onScale">
  7. <image class="image" :src="img" :data-index="i" :data-src="img" mode="widthFix" @touchmove="handletouchmove" @touchstart="handletouchstart" @touchend="handletouchend" />
  8. </movable-view>
  9. </movable-area>
  10. </swiper-item>
  11. </swiper>
  12. <view v-if="imgs.length > 0" class="page">
  13. <text class="text">
  14. {{ index+1 }} / {{ imgs.length }}
  15. </text>
  16. </view>
  17. <view v-if="descs.length > 0 && descs.length == imgs.length&&descs[index].length>0" class="desc">
  18. {{ descs[index] }}
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. /**
  24. * @see https://ext.dcloud.net.cn/plugin?id=2416
  25. */
  26. export default {
  27. name: 'WkPreveiwImg',
  28. props: {
  29. imgs: {// 图片列表
  30. type: Array,
  31. required: true,
  32. default: () => {
  33. return [];
  34. }
  35. },
  36. descs: {// 描述列表
  37. type: Array,
  38. required: false,
  39. default: () => {
  40. return [];
  41. }
  42. },
  43. // 透明度,0到1之间。
  44. opacity: {
  45. type: Number,
  46. default: 0.8
  47. }
  48. },
  49. data() {
  50. return {
  51. show: false, // 显示状态
  52. index: 0, // 当前页
  53. time: 0, // 定时器
  54. interval: 1000, // 长按事件
  55. scale: 1, // 缩放比例
  56. old: {
  57. scale: 1 // 缩放比例
  58. }
  59. };
  60. },
  61. methods: {
  62. onScale(e) {
  63. this.old.scale = e.detail.scale
  64. },
  65. // 接触开始
  66. handletouchstart(e) {
  67. var tchs = e.touches.length;
  68. if (tchs != 1) {
  69. return false;
  70. }
  71. this.time = setTimeout(() => {
  72. this.onLongPress(e);
  73. }, this.interval);
  74. return false;
  75. },
  76. // 清除定时器
  77. handletouchend() {
  78. clearTimeout(this.time);
  79. if (this.time != 0) {
  80. // 处理点击时间
  81. }
  82. return false;
  83. },
  84. // 清除定时器
  85. handletouchmove() {
  86. clearTimeout(this.time);
  87. this.time = 0;
  88. },
  89. // 处理长按事件
  90. onLongPress(e) {
  91. var src = e.currentTarget.dataset.src;
  92. var index = e.currentTarget.dataset.index;
  93. var data = {src: src, index: index}
  94. this.$emit('longPress', data);
  95. },
  96. // 图片改变
  97. change(e) {
  98. this.scale = 1;
  99. this.index = e.target.current;
  100. },
  101. // 打开
  102. open(e) {
  103. if (e === null || e === '') {
  104. return;
  105. }
  106. if (!isNaN(e)) {
  107. this.index = e;
  108. } else {
  109. this.index = this.imgs.indexOf(e);
  110. }
  111. console.log(this.index)
  112. this.show = true;
  113. },
  114. // 关闭
  115. close(e) {
  116. this.show = false;
  117. }
  118. }
  119. };
  120. </script>
  121. <style lang="scss" scoped>
  122. .previewImage {
  123. z-index: 999;
  124. position: fixed;
  125. top: 0;
  126. left: 0;
  127. width: 100%;
  128. height: 100%;
  129. background-color: #000000;
  130. user-select: none;
  131. .swiper {
  132. width: 100%;
  133. height: 100%;
  134. .marea {
  135. height: 100%;
  136. width: 100%;
  137. position: fixed;
  138. overflow: hidden;
  139. .mview {
  140. display: flex;
  141. align-items: center;
  142. justify-content: center;
  143. width: 100%;
  144. height: 100%;
  145. .image {
  146. width: 100%;
  147. }
  148. }
  149. }
  150. }
  151. .page {
  152. position: absolute;
  153. width: 100%;
  154. bottom: 20rpx;
  155. text-align: center;
  156. .text {
  157. color: #fff;
  158. font-size: 26rpx;
  159. background-color: rgba(0, 0, 0, 0.5);
  160. padding: 3rpx 16rpx;
  161. border-radius: 20rpx;
  162. }
  163. }
  164. .desc {
  165. position: absolute;
  166. top: 0;
  167. width: 100%;
  168. padding: 5rpx 10rpx;
  169. text-align: center;
  170. overflow: hidden;
  171. text-overflow: ellipsis;
  172. white-space: nowrap;
  173. background-color: rgba(0, 0, 0, 0.5);
  174. color: #fff;
  175. font-size: 28rpx;
  176. letter-spacing: 3rpx;
  177. }
  178. }
  179. </style>