index.vue 3.5 KB

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