albumCarouselItem.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <template>
  2. <div class="carousel-container">
  3. <!-- 图片列表 -->
  4. <div class="carousel-wrapper" :style="carouselStyle">
  5. <photo-provider v-if="images" :photo-closable="true" @visibleChange="handleVisibleChange">
  6. <!-- <photo-consumer
  7. class="carousel-img"
  8. v-for="(photo, index) in images"
  9. :key="photo.id"
  10. :src="base_img_url2 + (photo.cloudFilename ? photo.cloudFilename : photo)"
  11. >
  12. <img
  13. :index="index"
  14. :src="base_img_url2 + (photo.cloudFilename ? photo.cloudFilename : photo)"
  15. alt=""
  16. />
  17. </photo-consumer> -->
  18. <template v-for="(photo, index) in images"
  19. :key="photo.id">
  20. <album-draw-box :isShowNum="0" :farmId="766" :photo="photo" :current="currentIndex" :index="index" :length="images.length"
  21. ></album-draw-box>
  22. </template>
  23. </photo-provider>
  24. </div>
  25. <div class="label-text" v-if="labelText">{{ labelText }}</div>
  26. <!-- 左右箭头 -->
  27. <div @click.stop="prev" v-if="currentIndex !== 0" class="arrow left-arrow">
  28. <el-icon color="#F0D09C"><ArrowLeftBold /></el-icon>
  29. </div>
  30. <div @click.stop="next" v-if="images && currentIndex !== images.length - 1" class="arrow right-arrow">
  31. <el-icon color="#F0D09C"><ArrowRightBold /></el-icon>
  32. </div>
  33. </div>
  34. </template>
  35. <script setup>
  36. import { toRefs, ref, computed, onMounted, onUnmounted } from "vue";
  37. import AlbumDrawBox from "./albumDrawBox.vue";
  38. import { base_img_url2 } from "@/api/config";
  39. import "./cacheImg.js";
  40. const props = defineProps({
  41. images: {
  42. type: Array,
  43. required: true,
  44. },
  45. labelText: {
  46. type: String,
  47. default: "",
  48. },
  49. });
  50. const { images, labelText } = toRefs(props);
  51. let timer = null;
  52. const currentIndex = ref(0);
  53. onMounted(() => {
  54. updateImagePosition();
  55. clearAndRestartTimer();
  56. });
  57. onUnmounted(() => {
  58. clearInterval(timer);
  59. });
  60. const updateImagePosition = () => {
  61. carouselStyle.value.transform = `translateX(-${currentIndex.value * 100}%)`;
  62. };
  63. const clickPhotoShow = () => {
  64. if (timer) {
  65. clearInterval(timer);
  66. }
  67. };
  68. // 图片显隐切换回调
  69. const handleVisibleChange = ({ visible }) => {
  70. if (visible.value) {
  71. if (timer) {
  72. clearInterval(timer);
  73. }
  74. } else {
  75. clearAndRestartTimer();
  76. }
  77. };
  78. // 计算轮播图样式
  79. const carouselStyle = computed(() => {
  80. return {
  81. transform: `translateX(-${currentIndex.value * 100}%)`,
  82. };
  83. });
  84. // 下一张图片
  85. const next = () => {
  86. // 图片总数
  87. const totalImages = images.value.length;
  88. currentIndex.value = (currentIndex.value + 1) % totalImages;
  89. updateImagePosition();
  90. clearAndRestartTimer();
  91. };
  92. // 上一张图片
  93. const prev = () => {
  94. // 图片总数
  95. const totalImages = images.value.length;
  96. currentIndex.value = (currentIndex.value - 1 + totalImages) % totalImages;
  97. updateImagePosition();
  98. clearAndRestartTimer();
  99. };
  100. const clearAndRestartTimer = () => {
  101. if (timer) {
  102. clearInterval(timer);
  103. }
  104. // timer = setInterval(next, 5000);
  105. };
  106. </script>
  107. <style lang="scss" scoped>
  108. @import "src/styles/index";
  109. .carousel-container {
  110. position: relative;
  111. width: 100%;
  112. overflow: hidden;
  113. margin: 0 auto;
  114. .carousel-wrapper {
  115. display: flex;
  116. transition: transform 0.5s ease;
  117. width: 100%;
  118. .carousel-img {
  119. min-width: 100%;
  120. img {
  121. min-width: 100%;
  122. height: 255px;
  123. object-fit: cover;
  124. }
  125. }
  126. }
  127. .label-text {
  128. position: absolute;
  129. top: 0;
  130. left: 0;
  131. padding: 4px 10px;
  132. background: rgba(54, 52, 52, 0.8);
  133. color: #fff;
  134. font-size: 12px;
  135. border-radius: 8px 0 8px 0;
  136. z-index: 1;
  137. }
  138. .blur-bg {
  139. position: absolute;
  140. top: 0;
  141. width: 100%;
  142. height: 100%;
  143. backdrop-filter: blur(1.4px);
  144. .blur-content {
  145. border-radius: 8px;
  146. background: rgba(0, 0, 0, 0.5);
  147. width: 100%;
  148. height: 100%;
  149. display: flex;
  150. flex-direction: column;
  151. align-items: center;
  152. justify-content: center;
  153. font-size: 12px;
  154. color: #fff;
  155. .blur-img {
  156. img {
  157. width: 54px;
  158. position: relative;
  159. left: 4px;
  160. top: 4px;
  161. }
  162. }
  163. .blur-text {
  164. padding: 8px 0;
  165. text-align: center;
  166. line-height: 1.5;
  167. }
  168. .blur-btn {
  169. padding: 0 40px;
  170. box-shadow: 0 -2px 2px #86c9ff;
  171. height: 28px;
  172. line-height: 28px;
  173. border-radius: 50px;
  174. background: rgba(33, 153, 248, 0.7);
  175. // background: linear-gradient(#86C9FF, rgba(255, 255, 255, 0));
  176. }
  177. }
  178. }
  179. .arrow {
  180. position: absolute;
  181. top: 50%;
  182. transform: translateY(-50%);
  183. background: rgba(0, 0, 0, 0.5);
  184. width: rpx(72);
  185. height: rpx(72);
  186. border-radius: 50%;
  187. display: inline-flex;
  188. align-items: center;
  189. justify-content: center;
  190. cursor: pointer;
  191. pointer-events: all;
  192. }
  193. .left-arrow {
  194. left: rpx(32);
  195. }
  196. .right-arrow {
  197. right: rpx(32);
  198. }
  199. }
  200. </style>