ThreeDSwiper.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <template>
  2. <view class="three-item-swiper">
  3. <!-- 轮播容器 -->
  4. <view class="swiper-container" :style="{ height: containerHeight + 'px' }">
  5. <view class="swiper-wrapper"
  6. :style="{
  7. transform: `translateX(${translateX}px)`,
  8. transition: isAnimating ? 'transform 0.3s ease-out' : 'none'
  9. }">
  10. <!-- 轮播项 -->
  11. <view class="swiper-item"
  12. v-for="(item, index) in items"
  13. :key="index"
  14. :style="getItemStyle(index)">
  15. <image :src="item.imgUrl" mode="cover" class="item-image" :alt="item.title"></image>
  16. <view class="item-title" v-if="item.title">{{ item.title }}</view>
  17. </view>
  18. </view>
  19. </view>
  20. <!-- 指示点 -->
  21. <view class="indicators">
  22. <view class="indicator"
  23. v-for="(item, index) in items"
  24. :key="index"
  25. :class="{ active: index === currentIndex }"
  26. @click="switchTo(index)"></view>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. export default {
  32. props: {
  33. // 轮播数据
  34. items: {
  35. type: Array,
  36. required: true,
  37. default: () => []
  38. },
  39. // 容器高度(px)
  40. containerHeight: {
  41. type: Number,
  42. default: 400
  43. },
  44. // 自动轮播间隔(ms),0为不自动轮播
  45. autoPlay: {
  46. type: Number,
  47. default: 3000
  48. },
  49. // 中间项放大比例
  50. scaleRatio: {
  51. type: Number,
  52. default: 1.2
  53. }
  54. },
  55. data() {
  56. return {
  57. currentIndex: 0, // 当前居中索引
  58. startX: 0, // 触摸起始X
  59. moveX: 0, // 移动X
  60. translateX: 0, // 容器偏移量
  61. itemWidth: 0, // 项宽度
  62. isAnimating: false, // 是否动画中
  63. isDragging: false, // 是否拖拽中
  64. timer: null // 自动播放定时器
  65. };
  66. },
  67. watch: {
  68. items() {
  69. this.currentIndex = 0;
  70. this.$nextTick(() => {
  71. this.calculateLayout();
  72. });
  73. this.resetAutoPlay();
  74. },
  75. currentIndex() {
  76. this.updatePosition(true);
  77. },
  78. autoPlay() {
  79. this.resetAutoPlay();
  80. }
  81. },
  82. mounted() {
  83. // 初始化布局
  84. this.$nextTick(() => {
  85. this.calculateLayout();
  86. });
  87. // 监听窗口尺寸变化
  88. uni.onWindowResize(() => {
  89. this.$nextTick(() => {
  90. this.calculateLayout();
  91. });
  92. });
  93. // 初始化自动播放
  94. this.initAutoPlay();
  95. },
  96. beforeDestroy() {
  97. this.clearTimer();
  98. uni.offWindowResize();
  99. },
  100. methods: {
  101. // 计算布局
  102. calculateLayout() {
  103. // 获取容器宽度
  104. const query = uni.createSelectorQuery().in(this);
  105. query.select('.swiper-container').boundingClientRect(data => {
  106. if (data) {
  107. // 计算每个项的宽度(容器的一半)
  108. this.itemWidth = data.width / 2;
  109. this.updatePosition(false);
  110. }
  111. }).exec();
  112. },
  113. // 更新位置
  114. updatePosition(animate = true) {
  115. if (!this.itemWidth) return;
  116. this.isAnimating = animate;
  117. // 计算偏移量,让当前项居中
  118. this.translateX = this.itemWidth - this.currentIndex * this.itemWidth;
  119. },
  120. // 获取每个项的样式
  121. getItemStyle(index) {
  122. if (!this.itemWidth) return {};
  123. // 计算与当前项的距离
  124. const distance = Math.abs(index - this.currentIndex);
  125. let scale = 1;
  126. let zIndex = 1;
  127. let opacity = 0.8;
  128. // 中间项放大
  129. if (distance === 0) {
  130. scale = this.scaleRatio;
  131. zIndex = 10;
  132. opacity = 1;
  133. }
  134. return {
  135. width: `${this.itemWidth}px`,
  136. transform: `scale(${scale})`,
  137. zIndex,
  138. opacity,
  139. transition: this.isAnimating ? 'all 0.3s ease-out' : 'none'
  140. };
  141. },
  142. // 触摸开始
  143. handleTouchStart(e) {
  144. if (this.items.length <= 1) return;
  145. this.isDragging = true;
  146. this.isAnimating = false;
  147. this.startX = e.touches[0].clientX;
  148. this.clearTimer(); // 停止自动播放
  149. },
  150. // 触摸移动
  151. handleTouchMove(e) {
  152. if (!this.isDragging || this.items.length <= 1) return;
  153. this.moveX = e.touches[0].clientX;
  154. const diffX = this.moveX - this.startX;
  155. // 计算临时偏移量(增加阻力感)
  156. this.translateX = (this.itemWidth - this.currentIndex * this.itemWidth) + diffX * 0.8;
  157. },
  158. // 触摸结束
  159. handleTouchEnd() {
  160. if (!this.isDragging || this.items.length <= 1) return;
  161. this.isDragging = false;
  162. const diffX = this.moveX - this.startX;
  163. const threshold = this.itemWidth / 3; // 滑动阈值
  164. // 判断滑动方向
  165. if (diffX > threshold) {
  166. // 向右滑动,上一项
  167. this.prev();
  168. } else if (diffX < -threshold) {
  169. // 向左滑动,下一项
  170. this.next();
  171. } else {
  172. // 未达到阈值,回弹
  173. this.updatePosition(true);
  174. }
  175. this.resetAutoPlay(); // 恢复自动播放
  176. },
  177. // 下一项
  178. next() {
  179. if (this.currentIndex >= this.items.length - 1) {
  180. this.currentIndex = 0; // 循环到第一项
  181. } else {
  182. this.currentIndex++;
  183. }
  184. },
  185. // 上一项
  186. prev() {
  187. if (this.currentIndex <= 0) {
  188. this.currentIndex = this.items.length - 1; // 循环到最后一项
  189. } else {
  190. this.currentIndex--;
  191. }
  192. },
  193. // 切换到指定索引
  194. switchTo(index) {
  195. if (index === this.currentIndex) return;
  196. this.currentIndex = index;
  197. this.resetAutoPlay();
  198. },
  199. // 初始化自动播放
  200. initAutoPlay() {
  201. if (this.autoPlay > 0 && this.items.length > 1) {
  202. this.timer = setInterval(() => {
  203. this.next();
  204. }, this.autoPlay);
  205. }
  206. },
  207. // 重置自动播放
  208. resetAutoPlay() {
  209. this.clearTimer();
  210. this.initAutoPlay();
  211. },
  212. // 清除定时器
  213. clearTimer() {
  214. if (this.timer) {
  215. clearInterval(this.timer);
  216. this.timer = null;
  217. }
  218. }
  219. }
  220. };
  221. </script>
  222. <style scoped>
  223. .three-item-swiper {
  224. position: relative;
  225. width: 100%;
  226. overflow: hidden;
  227. }
  228. /* 轮播容器 */
  229. .swiper-container {
  230. position: relative;
  231. width: 100%;
  232. overflow: hidden;
  233. }
  234. /* 轮播轨道 */
  235. .swiper-wrapper {
  236. position: absolute;
  237. top: 0;
  238. left: 0;
  239. height: 100%;
  240. display: flex;
  241. }
  242. /* 轮播项 */
  243. .swiper-item {
  244. height: 100%;
  245. flex-shrink: 0;
  246. display: flex;
  247. flex-direction: column;
  248. align-items: center;
  249. justify-content: center;
  250. position: relative;
  251. }
  252. /* 图片样式 */
  253. .item-image {
  254. width: 100%;
  255. height: 100%;
  256. border-radius: 16rpx;
  257. box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.2);
  258. }
  259. /* 标题样式 */
  260. .item-title {
  261. position: absolute;
  262. bottom: 20rpx;
  263. left: 0;
  264. right: 0;
  265. text-align: center;
  266. color: #fff;
  267. font-size: 32rpx;
  268. text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.5);
  269. padding: 0 20rpx;
  270. white-space: nowrap;
  271. overflow: hidden;
  272. text-overflow: ellipsis;
  273. }
  274. /* 指示点容器 */
  275. .indicators {
  276. display: flex;
  277. justify-content: center;
  278. gap: 12rpx;
  279. padding: 20rpx 0;
  280. }
  281. /* 指示点样式 */
  282. .indicator {
  283. width: 16rpx;
  284. height: 16rpx;
  285. border-radius: 50%;
  286. background-color: #ddd;
  287. transition: all 0.3s ease;
  288. }
  289. /* 激活状态指示点 */
  290. .indicator.active {
  291. width: 40rpx;
  292. border-radius: 8rpx;
  293. background-color: #007aff;
  294. }
  295. </style>