u-swiper-indicator.vue 2.7 KB

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