u-swiper-indicator.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <view class="u-swiper-indicator">
  3. <view
  4. class="u-swiper-indicator__wrapper"
  5. v-if="indicatorMode === 'line'"
  6. :class="[`u-swiper-indicator__wrapper--${indicatorMode}`]"
  7. :style="{
  8. width: $u.addUnit(lineWidth * length),
  9. backgroundColor: indicatorInactiveColor
  10. }"
  11. >
  12. <view
  13. class="u-swiper-indicator__wrapper--line__bar"
  14. :style="[lineStyle]"
  15. ></view>
  16. </view>
  17. <view
  18. class="u-swiper-indicator__wrapper"
  19. v-if="indicatorMode === 'dot'"
  20. >
  21. <view
  22. class="u-swiper-indicator__wrapper__dot"
  23. v-for="(item, index) in length"
  24. :key="index"
  25. :class="[index === current && 'u-swiper-indicator__wrapper__dot--active']"
  26. :style="[dotStyle(index)]"
  27. >
  28. </view>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. /**
  34. * SwiperIndicator 轮播图指示器
  35. * @description 该组件一般用于导航轮播,广告展示等场景,可开箱即用,
  36. * @tutorial https://www.uviewui.com/components/swiper.html
  37. * @property {String | Number} length 轮播的长度(默认 0 )
  38. * @property {String | Number} current 当前处于活动状态的轮播的索引(默认 0 )
  39. * @property {String} indicatorActiveColor 指示器非激活颜色
  40. * @property {String} indicatorInactiveColor 指示器的激活颜色
  41. * @property {String} indicatorMode 指示器模式(默认 'line' )
  42. * @example <u-swiper :list="list4" indicator keyName="url" :autoplay="false"></u-swiper>
  43. */
  44. export default {
  45. name: 'u-swiper-indicator',
  46. props:{
  47. // 轮播的长度
  48. length: {
  49. type: [String, Number],
  50. default: 0
  51. },
  52. // 当前处于活动状态的轮播的索引
  53. current: {
  54. type: [String, Number],
  55. default: 0
  56. },
  57. // 指示器非激活颜色
  58. indicatorActiveColor: {
  59. type: String,
  60. default: ''
  61. },
  62. // 指示器的激活颜色
  63. indicatorInactiveColor: {
  64. type: String,
  65. default: ''
  66. },
  67. // 指示器模式,line-线型,dot-点型
  68. indicatorMode: {
  69. type: String,
  70. default: 'line'
  71. }
  72. },
  73. data() {
  74. return {
  75. lineWidth: 22
  76. }
  77. },
  78. computed: {
  79. // 指示器为线型的样式
  80. lineStyle() {
  81. let style = {}
  82. style.width = uni.$u.addUnit(this.lineWidth)
  83. style.transform = `translateX(${ uni.$u.addUnit(this.current * this.lineWidth) })`
  84. style.backgroundColor = this.indicatorActiveColor
  85. return style
  86. },
  87. // 指示器为点型的样式
  88. dotStyle() {
  89. return index => {
  90. let style = {}
  91. style.backgroundColor = index === this.current ? this.indicatorActiveColor : this.indicatorInactiveColor
  92. return style
  93. }
  94. }
  95. },
  96. }
  97. </script>
  98. <style lang="scss" scoped>
  99. @import "../../../libs/css/style.components.scss";
  100. .u-swiper-indicator {
  101. &__wrapper {
  102. @include vue-flex;
  103. &--line {
  104. border-radius: 100px;
  105. height: 4px;
  106. &__bar {
  107. width: 22px;
  108. height: 4px;
  109. border-radius: 100px;
  110. background-color: #FFFFFF;
  111. transition: transform 0.3s;
  112. }
  113. }
  114. &__dot {
  115. width: 5px;
  116. height: 5px;
  117. border-radius: 100px;
  118. margin: 0 4px;
  119. &--active {
  120. width: 12px;
  121. }
  122. }
  123. }
  124. }
  125. </style>