albumCarouselItem.vue 4.7 KB

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