index.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 mode="widthFix" class="slide-image" :src="dataItem.imgUrl" @click="onLink(dataItem.link)"
  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. indicatorDots: false, // 是否显示面板指示点
  40. autoplay: true, // 是否自动切换
  41. duration: 800, // 滑动动画时长
  42. imgHeights: [], // 图片的高度
  43. imgCurrent: 0, // 当前banne所在滑块指针
  44. }
  45. },
  46. /**
  47. * 组件的方法列表
  48. * 更新属性和数据的方法与更新页面数据的方法类似
  49. */
  50. methods: {
  51. /**
  52. * 计算图片高度
  53. */
  54. _imagesHeight(e) {
  55. // 获取图片真实宽度
  56. const imgwidth = e.detail.width,
  57. imgheight = e.detail.height,
  58. // 宽高比
  59. ratio = imgwidth / imgheight
  60. // 计算的高度值
  61. const viewHeight = 750 / ratio,
  62. imgHeights = this.imgHeights;
  63. // 把每一张图片的高度记录到数组里
  64. imgHeights.push(viewHeight);
  65. this.imgHeights = imgHeights
  66. },
  67. /**
  68. * 记录当前指针
  69. */
  70. _bindChange(e) {
  71. this.imgCurrent = e.detail.current
  72. }
  73. }
  74. }
  75. </script>
  76. <style lang="scss" scoped>
  77. .diy-banner {
  78. position: relative;
  79. // swiper组件
  80. .swiper-box {
  81. height: 100%;
  82. .slide-image {
  83. width: 100%;
  84. height: 100%;
  85. margin: 0 auto;
  86. display: block;
  87. }
  88. }
  89. /* 指示点 */
  90. .indicator-dots {
  91. width: 100%;
  92. height: 28rpx;
  93. padding: 0 20rpx;
  94. position: absolute;
  95. left: 0;
  96. right: 0;
  97. bottom: 20rpx;
  98. opacity: 0.8;
  99. display: flex;
  100. justify-content: center;
  101. .dots-item {
  102. width: 16rpx;
  103. height: 16rpx;
  104. margin-right: 8rpx;
  105. background-color: #fff;
  106. &:last-child {
  107. margin-right: 0;
  108. }
  109. &.active {
  110. background-color: #313131 !important;
  111. }
  112. }
  113. // 圆形
  114. &.round .dots-item {
  115. width: 16rpx;
  116. height: 16rpx;
  117. border-radius: 20rpx;
  118. }
  119. // 正方形
  120. &.square .dots-item {
  121. width: 16rpx;
  122. height: 16rpx;
  123. }
  124. // 长方形
  125. &.rectangle .dots-item {
  126. width: 22rpx;
  127. height: 14rpx;
  128. }
  129. }
  130. }
  131. </style>