index.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <view class="diy-banner" :style="{ height: `${imgHeights[imgCurrent]}rpx` }">
  3. <!-- 图片轮播 -->
  4. <swiper :autoplay="autoplay" class="swiper-box" :duration="duration" :circular="true" :interval="itemStyle.interval * 1000"
  5. @change="_bindChange">
  6. <swiper-item v-for="(dataItem, index) in dataList" :key="index">
  7. <image lazy-load :lazy-load-margin="0" class="slide-image" :src="dataItem.image" @click="onLink(dataItem.url)"
  8. @load="_imagesHeight" />
  9. </swiper-item>
  10. </swiper>
  11. <!-- 指示点 -->
  12. <view class="indicator-dots" :class="itemStyle.btnShape">
  13. <view class="dots-item" :class="{ active: imgCurrent == 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. name: "Banner",
  22. /**
  23. * 组件的属性列表
  24. * 用于组件自定义设置
  25. */
  26. props: {
  27. itemIndex: String,
  28. itemStyle: Object,
  29. params: Object,
  30. dataList: Array
  31. },
  32. mixins: [mixin],
  33. /**
  34. * 私有数据,组件的初始数据
  35. * 可用于模版渲染
  36. */
  37. data() {
  38. return {
  39. width: 730,
  40. indicatorDots: false, // 是否显示面板指示点
  41. autoplay: true, // 是否自动切换
  42. duration: 800, // 滑动动画时长
  43. imgHeights: [], // 图片的高度
  44. imgCurrent: 0, // 当前banne所在滑块指针
  45. }
  46. },
  47. /**
  48. * 组件的方法列表
  49. * 更新属性和数据的方法与更新页面数据的方法类似
  50. */
  51. methods: {
  52. onLink(linkObj) {
  53. this.$navTo(linkObj)
  54. },
  55. /**
  56. * 计算图片高度
  57. */
  58. _imagesHeight(e) {
  59. // 获取图片真实宽度
  60. const imgwidth = e.detail.width,
  61. imgheight = e.detail.height,
  62. // 宽高比
  63. ratio = imgwidth / imgheight;
  64. // 计算的高度值
  65. const viewHeight = this.width / ratio;
  66. const imgHeights = this.imgHeights;
  67. // 把每一张图片的高度记录到数组里
  68. imgHeights.push(viewHeight);
  69. this.imgHeights = imgHeights;
  70. },
  71. /**
  72. * 记录当前指针
  73. */
  74. _bindChange(e) {
  75. this.imgCurrent = e.detail.current
  76. }
  77. }
  78. }
  79. </script>
  80. <style lang="scss" scoped>
  81. .diy-banner {
  82. position: relative;
  83. max-height: 400rpx;
  84. margin-top: 100rpx;
  85. /* #ifdef H5 */
  86. margin-top: 120rpx;
  87. /* #endif */
  88. // swiper组件
  89. .swiper-box {
  90. height: 100%;
  91. padding: 0rpx 10rpx 0rpx 10rpx;
  92. max-width: 750rpx;
  93. max-height: 450rpx;
  94. .slide-image {
  95. width: 100%;
  96. height: 100%;
  97. margin: 0 auto;
  98. display: block;
  99. border-radius: 10rpx;
  100. }
  101. }
  102. /* 指示点 */
  103. .indicator-dots {
  104. width: 100%;
  105. height: 28rpx;
  106. padding: 0 20rpx;
  107. position: absolute;
  108. left: 0;
  109. right: 0;
  110. bottom: 20rpx;
  111. opacity: 0.8;
  112. display: flex;
  113. justify-content: center;
  114. .dots-item {
  115. width: 16rpx;
  116. height: 16rpx;
  117. margin-right: 8rpx;
  118. background-color: #fff;
  119. &:last-child {
  120. margin-right: 0;
  121. }
  122. &.active {
  123. background-color: #313131 !important;
  124. }
  125. }
  126. // 圆形
  127. &.round .dots-item {
  128. width: 16rpx;
  129. height: 16rpx;
  130. border-radius: 20rpx;
  131. }
  132. // 正方形
  133. &.square .dots-item {
  134. width: 16rpx;
  135. height: 16rpx;
  136. }
  137. // 长方形
  138. &.rectangle .dots-item {
  139. width: 22rpx;
  140. height: 14rpx;
  141. }
  142. }
  143. }
  144. </style>