u-swiper-indicator.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. import props from './props.js';
  34. /**
  35. * SwiperIndicator 轮播图指示器
  36. * @description 该组件一般用于导航轮播,广告展示等场景,可开箱即用,
  37. * @tutorial https://www.uviewui.com/components/swiper.html
  38. * @property {String | Number} length 轮播的长度(默认 0 )
  39. * @property {String | Number} current 当前处于活动状态的轮播的索引(默认 0 )
  40. * @property {String} indicatorActiveColor 指示器非激活颜色
  41. * @property {String} indicatorInactiveColor 指示器的激活颜色
  42. * @property {String} indicatorMode 指示器模式(默认 'line' )
  43. * @example <u-swiper :list="list4" indicator keyName="url" :autoplay="false"></u-swiper>
  44. */
  45. export default {
  46. name: 'u-swiper-indicator',
  47. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  48. data() {
  49. return {
  50. lineWidth: 22
  51. }
  52. },
  53. computed: {
  54. // 指示器为线型的样式
  55. lineStyle() {
  56. let style = {}
  57. style.width = uni.$u.addUnit(this.lineWidth)
  58. style.transform = `translateX(${ uni.$u.addUnit(this.current * this.lineWidth) })`
  59. style.backgroundColor = this.indicatorActiveColor
  60. return style
  61. },
  62. // 指示器为点型的样式
  63. dotStyle() {
  64. return index => {
  65. let style = {}
  66. style.backgroundColor = index === this.current ? this.indicatorActiveColor : this.indicatorInactiveColor
  67. return style
  68. }
  69. }
  70. },
  71. }
  72. </script>
  73. <style lang="scss" scoped>
  74. @import "../../libs/css/components.scss";
  75. .u-swiper-indicator {
  76. &__wrapper {
  77. @include flex;
  78. &--line {
  79. border-radius: 100px;
  80. height: 4px;
  81. &__bar {
  82. width: 22px;
  83. height: 4px;
  84. border-radius: 100px;
  85. background-color: #FFFFFF;
  86. transition: transform 0.3s;
  87. }
  88. }
  89. &__dot {
  90. width: 5px;
  91. height: 5px;
  92. border-radius: 100px;
  93. margin: 0 4px;
  94. &--active {
  95. width: 12px;
  96. }
  97. }
  98. }
  99. }
  100. </style>