index.vue 3.1 KB

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