examplePopup.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <popup v-model:show="localShow" class="example-popup" :overlay-style="{ backdropFilter: 'blur(4px)' }">
  3. <div class="example-content">
  4. <div class="example-swipe-wrap">
  5. <Swipe
  6. ref="exampleSwipeRef"
  7. :show-indicators="false"
  8. v-model="activeIndex"
  9. :autoplay="0"
  10. indicator-color="#2199F8"
  11. >
  12. <SwipeItem v-for="img in images" :key="img">
  13. <div class="img-tag">{{ title }}</div>
  14. <img class="example-img" :src="img" alt="" />
  15. </SwipeItem>
  16. </Swipe>
  17. <!-- 左箭头 -->
  18. <div
  19. v-if="images.length > 1 && activeIndex > 0"
  20. class="example-arrow arrow-left"
  21. @click.stop="prevExample"
  22. >
  23. <el-icon>
  24. <ArrowLeftBold color="#F0D09C" />
  25. </el-icon>
  26. </div>
  27. <!-- 右箭头 -->
  28. <div
  29. v-if="images.length > 1 && activeIndex < images.length - 1"
  30. class="example-arrow arrow-right"
  31. @click.stop="nextExample"
  32. >
  33. <el-icon>
  34. <ArrowRightBold color="#F0D09C" />
  35. </el-icon>
  36. </div>
  37. </div>
  38. <div class="example-tips">
  39. {{ tips }}
  40. </div>
  41. </div>
  42. </popup>
  43. </template>
  44. <script setup>
  45. import { ref, watch, computed } from "vue";
  46. import { Popup, Swipe, SwipeItem } from "vant";
  47. const props = defineProps({
  48. show: {
  49. type: Boolean,
  50. default: false,
  51. },
  52. images: {
  53. type: Array,
  54. default: () => [],
  55. },
  56. startIndex: {
  57. type: Number,
  58. default: 0,
  59. },
  60. title: {
  61. type: String,
  62. default: "示例图",
  63. },
  64. tips: {
  65. type: String,
  66. default: "拍摄要求:请采集代表农场作物物候期的照片,请采集代表农场作物物候期的照片。",
  67. },
  68. });
  69. const emit = defineEmits(["update:show"]);
  70. const exampleSwipeRef = ref(null);
  71. const activeIndex = ref(0);
  72. // v-model:show 双向绑定
  73. const localShow = computed({
  74. get: () => props.show,
  75. set: (val) => emit("update:show", val),
  76. });
  77. // 当打开弹窗或 startIndex 变化时,设置当前索引
  78. watch(
  79. () => [props.show, props.startIndex],
  80. ([show, start]) => {
  81. if (show) {
  82. activeIndex.value = start || 0;
  83. if (exampleSwipeRef.value) {
  84. exampleSwipeRef.value.swipeTo(activeIndex.value);
  85. }
  86. }
  87. }
  88. );
  89. const prevExample = () => {
  90. if (!props.images.length) return;
  91. activeIndex.value = (activeIndex.value - 1 + props.images.length) % props.images.length;
  92. exampleSwipeRef.value && exampleSwipeRef.value.swipeTo(activeIndex.value);
  93. };
  94. const nextExample = () => {
  95. if (!props.images.length) return;
  96. activeIndex.value = (activeIndex.value + 1) % props.images.length;
  97. exampleSwipeRef.value && exampleSwipeRef.value.swipeTo(activeIndex.value);
  98. };
  99. </script>
  100. <style scoped lang="scss">
  101. .example-popup {
  102. width: 100%;
  103. border-radius: 0;
  104. background: none;
  105. max-width: 100%;
  106. .example-content {
  107. text-align: center;
  108. .example-swipe-wrap {
  109. position: relative;
  110. }
  111. .example-img {
  112. width: 100%;
  113. }
  114. .img-tag {
  115. position: absolute;
  116. top: 0;
  117. left: 0;
  118. padding: 0 10px;
  119. height: 28px;
  120. line-height: 28px;
  121. font-size: 12px;
  122. color: #fff;
  123. background: rgba(0, 0, 0, 0.7);
  124. border-radius: 0 0 10px 0;
  125. z-index: 2;
  126. }
  127. .example-arrow {
  128. position: absolute;
  129. top: 50%;
  130. transform: translateY(-50%);
  131. width: 32px;
  132. height: 32px;
  133. border-radius: 50%;
  134. background: rgba(0, 0, 0, 0.45);
  135. display: flex;
  136. align-items: center;
  137. justify-content: center;
  138. z-index: 3;
  139. }
  140. .arrow-left {
  141. left: 8px;
  142. }
  143. .arrow-right {
  144. right: 8px;
  145. }
  146. }
  147. .example-tips {
  148. margin: 16px 12px 6px 12px;
  149. background: #3d3d3d;
  150. padding: 8px 10px;
  151. border-radius: 4px;
  152. backdrop-filter: blur(4px);
  153. color: #fff;
  154. font-size: 14px;
  155. line-height: 21px;
  156. text-align: left;
  157. }
  158. }
  159. </style>