ThreeItemSwiper.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <view class="custom-swiper-wrapper">
  3. <swiper class="custom-swiper" :current="activeIndex" @change="onSwiperChange" circular :duration="300"
  4. indicator-dots="false" autoplay="false" display-multiple-items="1" :previous-margin="outerMargin"
  5. :next-margin="outerMargin" indicator-color="rgba(255, 255, 255, 0.5)" indicator-active-color="#FFEB66">
  6. <swiper-item v-for="(item, index) in products" :key="index">
  7. <!-- 用容器包裹item,通过padding控制内部间距 -->
  8. <view class="item-container">
  9. <!-- <view class="swiper-item" :class="{ 'swiper-item-active': index === currentCenterIndex }">
  10. <image class="item-image" :src="item.image" mode="aspectFit"></image>
  11. <view class="item-text" v-if="index === currentCenterIndex">
  12. <view class="text-title">{{ item.title }}</view>
  13. <view class="text-desc">{{ item.desc }}</view>
  14. </view>
  15. </view> -->
  16. <view class="swiper-item" :class="{ 'swiper-item-active': index === currentCenterIndex }">
  17. <image class="item-image" :src="item.imgUrl" mode="aspectFit"></image>
  18. <view class="item-text" v-if="index === currentCenterIndex">
  19. <view class="text-title">{{ item.prizeLevel }}等奖</view>
  20. <view class="text-desc">{{ item.productName }}</view>
  21. </view>
  22. </view>
  23. </view>
  24. </swiper-item>
  25. </swiper>
  26. </view>
  27. </template>
  28. <script>
  29. import {
  30. string
  31. } from 'uview-plus/libs/function/test';
  32. export default {
  33. props: {
  34. products: {
  35. type: Array,
  36. default: "",
  37. }
  38. },
  39. data() {
  40. return {
  41. activeIndex: 0, // 初始激活项索引(建议从1开始,避免循环时偏移)
  42. outerMargin: '120rpx', // 轮播左右外部间距
  43. // swiperData: [{
  44. // image: "/static/images/zfb.png",
  45. // title: "二等奖",
  46. // desc: "纳美科学高浓度小苏打牙膏",
  47. // },
  48. // {
  49. // image: "/static/images/red_bg.png",
  50. // title: "二等奖",
  51. // desc: "纳美科学高浓度小苏打牙膏",
  52. // },
  53. // {
  54. // image: "/static/images/share.png",
  55. // title: "二等奖",
  56. // desc: "纳美科学高浓度小苏打牙膏",
  57. // },
  58. // {
  59. // image: "/static/images/integral.png",
  60. // title: "二等奖",
  61. // desc: "纳美科学高浓度小苏打牙膏",
  62. // }
  63. // ],
  64. };
  65. },
  66. computed: {
  67. // 计算中心项索引(解决循环轮播时的索引匹配问题)
  68. currentCenterIndex() {
  69. if (this.products.length === 0) return 0;
  70. return this.activeIndex % this.products.length;
  71. }
  72. },
  73. methods: {
  74. onSwiperChange(e) {
  75. this.activeIndex = e.detail.current;
  76. },
  77. },
  78. };
  79. </script>
  80. <style scoped>
  81. .custom-swiper-wrapper {
  82. width: 100%;
  83. overflow: hidden;
  84. /* padding: 20rpx 0; */
  85. }
  86. .custom-swiper {
  87. width: 100%;
  88. height: 500rpx;
  89. /* 固定高度,避免item被截断 */
  90. }
  91. /* 控制item之间的间距容器 */
  92. .item-container {
  93. padding: 0 20rpx;
  94. /* 左右内边距,控制item之间的间距 */
  95. }
  96. /* 基础item样式 */
  97. .swiper-item {
  98. width: 100% !important;
  99. /* 宽度占满容器,配合padding控制实际宽度 */
  100. height: 348rpx;
  101. background: #FFFFFF;
  102. box-shadow: 0rpx 12rpx 19rpx 2rpx rgba(219, 73, 22, 0.6);
  103. border-radius: 24rpx;
  104. border: 4rpx solid #FFCA96;
  105. display: flex;
  106. flex-direction: column;
  107. align-items: center;
  108. box-sizing: border-box;
  109. padding: 20rpx 0;
  110. transition: all 0.3s ease;
  111. }
  112. /* 中心项激活样式 */
  113. .swiper-item-active {
  114. transition: all 0.5s ease;
  115. height: 420rpx;
  116. /* 中心项高度增加 */
  117. z-index: 10;
  118. /* 确保中心项在最上层 */
  119. }
  120. .item-image {
  121. width: 280rpx;
  122. height: 280rpx;
  123. }
  124. .item-text {
  125. text-align: center;
  126. }
  127. .text-title {
  128. font-weight: 500;
  129. font-size: 32rpx;
  130. color: #222222;
  131. margin: 20rpx 0 10rpx;
  132. }
  133. .text-desc {
  134. font-size: 24rpx;
  135. color: #757575;
  136. }
  137. </style>