kxj-previewImage.vue 3.5 KB

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