tui-gallery.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <view class="tui-gallery" :class="{'tui-gallery_show':show}" @tap="hideGallery">
  3. <view class="tui-gallery__info">{{currentIndex+1}}/{{getLen}}</view>
  4. <swiper class="tui-gallery__img__wrap" :indicator-dots="false" @change="change" :current="defCurIndex"
  5. :autoplay="false" :duration="500">
  6. <swiper-item v-for="(item,index) in imgUrls" :key="index">
  7. <image mode="aspectFit" class="tui-gallery__img" :src="item.src"></image>
  8. </swiper-item>
  9. </swiper>
  10. <view class="tui-gallery__desc" v-if="!showDelete">
  11. {{getDesc(currentIndex,imgUrls)}}
  12. </view>
  13. <view class="tui-gallery__operate" hover-class="tui-opacity__del" :hover-start-time="150" @tap.stop="deleteImg"
  14. v-if="showDelete">
  15. 删除
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. export default {
  21. name: 'tuiGallery',
  22. emits: ['change', 'delete', 'hide'],
  23. props: {
  24. urls: {
  25. type: Array,
  26. default () {
  27. return []
  28. }
  29. },
  30. showDelete: {
  31. type: Boolean,
  32. default: false
  33. },
  34. show: {
  35. type: Boolean,
  36. default: false
  37. },
  38. current: {
  39. type: Number,
  40. default: 0
  41. },
  42. hideOnClick: {
  43. type: Boolean,
  44. default: true
  45. }
  46. },
  47. computed: {
  48. getLen() {
  49. return this.imgUrls.length
  50. }
  51. },
  52. watch: {
  53. urls(newVal, oldVal) {
  54. this.imgUrls = newVal
  55. },
  56. current(newVal) {
  57. this.defCurIndex = this.currentIndex;
  58. let val = Number(newVal)
  59. setTimeout(() => {
  60. this.defCurIndex = val;
  61. this.currentIndex = val;
  62. }, 20)
  63. }
  64. },
  65. mounted() {
  66. this.defCurIndex = Number(this.current);
  67. this.currentIndex = this.defCurIndex;
  68. this.imgUrls = this.urls;
  69. },
  70. data() {
  71. return {
  72. imgUrls: [],
  73. currentIndex: 0,
  74. defCurIndex: 0
  75. };
  76. },
  77. methods: {
  78. getDesc(index, imgUrls) {
  79. let desc = ''
  80. let item = imgUrls[index]
  81. if (item) {
  82. desc = item.desc
  83. }
  84. return desc
  85. },
  86. change(e) {
  87. this.currentIndex = e.detail.current
  88. this.$emit('change', {
  89. current: e.detail.current
  90. });
  91. },
  92. deleteImg() {
  93. const imgs = this.imgUrls;
  94. const url = imgs.splice(this.current, 1);
  95. this.$emit('delete', {
  96. url: url[0],
  97. index: this.current
  98. });
  99. if (imgs.length === 0) {
  100. this.hideGallery();
  101. return;
  102. }
  103. this.current = 0;
  104. this.imgUrls = imgs
  105. },
  106. hideGallery() {
  107. if (this.hideOnClick) {
  108. this.$emit('hide', {});
  109. }
  110. }
  111. }
  112. }
  113. </script>
  114. <style scoped>
  115. .tui-gallery {
  116. position: fixed;
  117. top: 0;
  118. right: 0;
  119. bottom: 0;
  120. left: 0;
  121. background-color: #000;
  122. z-index: 1000;
  123. display: none;
  124. }
  125. .tui-gallery__img,
  126. .tui-gallery__operate,
  127. .tui-gallery__desc {
  128. position: absolute;
  129. left: 0;
  130. left: constant(safe-area-inset-left);
  131. left: env(safe-area-inset-left);
  132. right: 0;
  133. right: constant(safe-area-inset-right);
  134. right: env(safe-area-inset-right)
  135. }
  136. .tui-gallery__img {
  137. width: 100%;
  138. height: 100%;
  139. top: 0;
  140. top: constant(safe-area-inset-top);
  141. top: env(safe-area-inset-top);
  142. bottom: 60px;
  143. bottom: calc(60px + constant(safe-area-inset-bottom));
  144. bottom: calc(60px + env(safe-area-inset-bottom));
  145. background: 50% no-repeat;
  146. background-size: contain
  147. }
  148. .tui-gallery__operate,
  149. .tui-gallery__desc {
  150. position: absolute;
  151. bottom: 0;
  152. padding-bottom: 0;
  153. padding-bottom: constant(safe-area-inset-bottom);
  154. padding-bottom: env(safe-area-inset-bottom);
  155. background-color: #0d0d0d;
  156. color: #fff;
  157. line-height: 60px;
  158. text-align: center;
  159. white-space: nowrap;
  160. overflow: hidden;
  161. text-overflow: ellipsis;
  162. padding: 0 30rpx;
  163. box-sizing: border-box;
  164. z-index: 10;
  165. }
  166. .tui-gallery__info {
  167. color: #fff;
  168. font-size: 17px;
  169. line-height: 60px;
  170. min-height: 60px;
  171. text-align: center
  172. }
  173. .tui-gallery__img__wrap {
  174. -webkit-box-flex: 1;
  175. -webkit-flex: 1;
  176. flex: 1;
  177. position: relative;
  178. font-size: 0
  179. }
  180. .tui-gallery__operate {
  181. position: static
  182. }
  183. .tui-gallery_show {
  184. display: flex !important;
  185. flex-direction: column !important;
  186. flex-wrap: nowrap !important;
  187. }
  188. .tui-opacity__del {
  189. opacity: 0.5;
  190. }
  191. </style>