albumCarouselItem.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 :farmId="farmId" :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="lock && currentIndex !== 0">
  14. <div class="blur-content">
  15. <div class="blur-img">
  16. <img src="@/assets/img/gallery/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. farmId:{
  43. type: [Number,String],
  44. required: true
  45. },
  46. lock:{
  47. type: Boolean,
  48. default: true
  49. }
  50. })
  51. const {images} = toRefs(props);
  52. let timer = null;
  53. const currentIndex = ref(0);
  54. onMounted(() => {
  55. updateImagePosition();
  56. clearAndRestartTimer();
  57. });
  58. onUnmounted(() => {
  59. clearInterval(timer);
  60. });
  61. const updateImagePosition = () => {
  62. carouselStyle.value.transform = `translateX(-${currentIndex.value * 100}%)`;
  63. };
  64. const clickPhotoShow = () => {
  65. if (timer) {
  66. clearInterval(timer)
  67. };
  68. }
  69. // 图片显隐切换回调
  70. const handleVisibleChange = ({visible}) => {
  71. if (visible.value) {
  72. if (timer) {
  73. clearInterval(timer)
  74. }
  75. } else {
  76. clearAndRestartTimer();
  77. }
  78. }
  79. // 计算轮播图样式
  80. const carouselStyle = computed(() => {
  81. return {
  82. transform: `translateX(-${currentIndex.value * 100}%)`,
  83. };
  84. });
  85. // 下一张图片
  86. const next = () => {
  87. // 图片总数
  88. const totalImages = images.value.length;
  89. currentIndex.value = (currentIndex.value + 1) % totalImages;
  90. updateImagePosition();
  91. clearAndRestartTimer();
  92. };
  93. // 上一张图片
  94. const prev = () => {
  95. // 图片总数
  96. const totalImages = images.value.length;
  97. currentIndex.value = (currentIndex.value - 1 + totalImages) % totalImages;
  98. updateImagePosition();
  99. clearAndRestartTimer();
  100. };
  101. const clearAndRestartTimer = () => {
  102. if (timer) {
  103. clearInterval(timer);
  104. }
  105. // timer = setInterval(next, 5000);
  106. };
  107. </script>
  108. <style lang="scss" scoped>
  109. @import "src/styles/index";
  110. .carousel-container {
  111. position: relative;
  112. width: 100%;
  113. height: 100%;
  114. overflow: hidden;
  115. margin: 0 auto;
  116. .carousel-wrapper {
  117. display: flex;
  118. transition: transform 0.5s ease;
  119. width: 100%;
  120. }
  121. .blur-bg {
  122. position: absolute;
  123. top: 0;
  124. width: 100%;
  125. height: 100%;
  126. backdrop-filter: blur(1.4px);
  127. .blur-content {
  128. border-radius: 8px;
  129. background: rgba(0, 0, 0, 0.5);
  130. width: 100%;
  131. height: 100%;
  132. display: flex;
  133. flex-direction: column;
  134. align-items: center;
  135. justify-content: center;
  136. font-size: 12px;
  137. color: #fff;
  138. .blur-img {
  139. img {
  140. width: 54px;
  141. position: relative;
  142. left: 4px;
  143. top: 4px;
  144. }
  145. }
  146. .blur-text {
  147. padding: 8px 0;
  148. text-align: center;
  149. line-height: 1.5;
  150. }
  151. .blur-btn {
  152. padding: 0 40px;
  153. box-shadow: 0 -2px 2px #86C9FF;
  154. height: 28px;
  155. line-height: 28px;
  156. border-radius: 50px;
  157. background: rgba(33, 153, 248, 0.7);
  158. // background: linear-gradient(#86C9FF, rgba(255, 255, 255, 0));
  159. }
  160. }
  161. }
  162. .arrow {
  163. position: absolute;
  164. top: 50%;
  165. transform: translateY(-50%);
  166. background: rgba(0, 0, 0, 0.5);
  167. width: rpx(72);
  168. height: rpx(72);
  169. border-radius: 50%;
  170. display: inline-flex;
  171. align-items: center;
  172. justify-content: center;
  173. cursor: pointer;
  174. pointer-events: all;
  175. }
  176. .left-arrow {
  177. left: rpx(32);
  178. }
  179. .right-arrow {
  180. right: rpx(32);
  181. }
  182. }
  183. </style>