albumCarouselItem.vue 4.2 KB

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