index.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <view class="diy-banner"
  3. :style="{ padding: `${itemStyle.paddingTop * 2}rpx ${itemStyle.paddingLeft * 2}rpx`, background: itemStyle.background, height: `${imgHeights[swiperIndex]}px` }">
  4. <!-- 图片轮播 -->
  5. <swiper :autoplay="autoplay" class="swiper-box" :duration="duration" :circular="true" :interval="itemStyle.interval * 1000"
  6. @change="onChangeItem" :style="{ borderRadius: `${itemStyle.borderRadius * 2}rpx` }">
  7. <swiper-item class="swiper-item" v-for="(dataItem, index) in dataList" :key="index">
  8. <image mode="widthFix" class="slide-image" :src="dataItem.imgUrl" @click="onLink(dataItem.link)" @load="onLoadImage" />
  9. </swiper-item>
  10. </swiper>
  11. <!-- 指示点 -->
  12. <view class="indicator-dots" :class="itemStyle.btnShape" :style="{ '--padding-top': `${itemStyle.paddingTop * 2}rpx` }">
  13. <view class="dots-item" :class="{ active: swiperIndex == index }" :style="{ backgroundColor: itemStyle.btnColor }"
  14. v-for="(dataItem, index) in dataList" :key="index"></view>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. import mixin from '../mixin'
  20. export default {
  21. props: {
  22. itemIndex: String,
  23. itemStyle: Object,
  24. params: Object,
  25. dataList: Array
  26. },
  27. mixins: [mixin],
  28. data() {
  29. return {
  30. windowWidth: 750,
  31. indicatorDots: false, // 是否显示面板指示点
  32. autoplay: true, // 是否自动切换
  33. duration: 800, // 滑动动画时长
  34. imgHeights: [], // 每张图片对应的容器高度
  35. swiperIndex: 0 // 当前banne所在滑块指针
  36. }
  37. },
  38. created() {
  39. const { windowWidth } = uni.getWindowInfo()
  40. this.windowWidth = windowWidth > 750 ? 750 : windowWidth
  41. },
  42. methods: {
  43. // 计算图片高度
  44. onLoadImage({ detail }) {
  45. const app = this
  46. // 图片的宽高比
  47. const ratio = detail.width / detail.height
  48. // 计算容器高度值
  49. const viewHeight = (app.windowWidth - (app.itemStyle.paddingLeft * 2)) / ratio
  50. // 每张图片对应的容器高度
  51. app.imgHeights.push(viewHeight + (app.itemStyle.paddingTop * 2))
  52. },
  53. // 记录当前指针
  54. onChangeItem({ detail }) {
  55. this.swiperIndex = detail.current
  56. }
  57. }
  58. }
  59. </script>
  60. <style lang="scss" scoped>
  61. .diy-banner {
  62. position: relative;
  63. }
  64. // swiper组件
  65. .swiper-box {
  66. height: 100%;
  67. overflow: hidden;
  68. .swiper-item {}
  69. .slide-image {
  70. width: 100%;
  71. height: 100%;
  72. margin: 0 auto;
  73. display: block;
  74. }
  75. }
  76. // 指示点
  77. .indicator-dots {
  78. --padding-top: 0;
  79. height: 28rpx;
  80. padding: 0 20rpx;
  81. position: absolute;
  82. left: 0;
  83. right: 0;
  84. bottom: calc(var(--padding-top) + 20rpx);
  85. opacity: 0.8;
  86. display: flex;
  87. justify-content: center;
  88. align-items: center;
  89. .dots-item {
  90. margin-right: 8rpx;
  91. width: 16rpx;
  92. height: 16rpx;
  93. background-color: #fff;
  94. &:last-child {
  95. margin-right: 0;
  96. }
  97. &.active {
  98. background-color: #313131 !important;
  99. }
  100. }
  101. // 长方形
  102. &.rectangle .dots-item {
  103. width: 32rpx;
  104. height: 16rpx;
  105. }
  106. // 正方形
  107. &.square .dots-item {}
  108. // 圆形
  109. &.round .dots-item {
  110. border-radius: 20rpx;
  111. }
  112. }
  113. </style>