index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. // #ifdef H5
  42. // 此处取决于pages.json中的maxWidth
  43. this.windowWidth = windowWidth > 450 ? 450 : windowWidth
  44. // #endif
  45. },
  46. methods: {
  47. // 计算图片高度
  48. onLoadImage({ detail }) {
  49. const app = this
  50. // 图片的宽高比
  51. const ratio = detail.width / detail.height
  52. // 计算容器高度值
  53. const viewHeight = (app.windowWidth - (app.itemStyle.paddingLeft * 2)) / ratio
  54. // 每张图片对应的容器高度
  55. app.imgHeights.push(viewHeight + (app.itemStyle.paddingTop * 2))
  56. },
  57. // 记录当前指针
  58. onChangeItem({ detail }) {
  59. this.swiperIndex = detail.current
  60. }
  61. }
  62. }
  63. </script>
  64. <style lang="scss" scoped>
  65. .diy-banner {
  66. position: relative;
  67. }
  68. // swiper组件
  69. .swiper-box {
  70. height: 100%;
  71. overflow: hidden;
  72. .swiper-item {}
  73. .slide-image {
  74. width: 100%;
  75. height: 100%;
  76. margin: 0 auto;
  77. display: block;
  78. }
  79. }
  80. // 指示点
  81. .indicator-dots {
  82. --padding-top: 0;
  83. height: 28rpx;
  84. padding: 0 20rpx;
  85. position: absolute;
  86. left: 0;
  87. right: 0;
  88. bottom: calc(var(--padding-top) + 20rpx);
  89. opacity: 0.8;
  90. display: flex;
  91. justify-content: center;
  92. align-items: center;
  93. .dots-item {
  94. margin-right: 8rpx;
  95. width: 16rpx;
  96. height: 16rpx;
  97. background-color: #fff;
  98. &:last-child {
  99. margin-right: 0;
  100. }
  101. &.active {
  102. background-color: #313131 !important;
  103. }
  104. }
  105. // 长方形
  106. &.rectangle .dots-item {
  107. width: 32rpx;
  108. height: 16rpx;
  109. }
  110. // 正方形
  111. &.square .dots-item {}
  112. // 圆形
  113. &.round .dots-item {
  114. border-radius: 20rpx;
  115. }
  116. }
  117. </style>