You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.8 KiB

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