index.vue 3.4 KB

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