albumCarouselItem.vue 4.7 KB

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