uni-swiper-dot.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <view class="uni-swiper__warp">
  3. <slot></slot>
  4. <view v-if="mode === 'default'" class="uni-swiper__dots-box" :style="{'bottom':dots.bottom + 'px'}">
  5. <view class="uni-swiper__dots-item" v-for="(item,index) in info" :style="{
  6. 'width': dots.width + 'px','height':dots.height +'px' ,'background-color':index !== current?dots.backgroundColor:dots.selectedBackgroundColor,'border':index !==current ? dots.border:dots.selectedBorder}"
  7. :key="index"></view>
  8. </view>
  9. <view v-if="mode === 'long'" class="uni-swiper__dots-box" :style="{'bottom':dots.bottom + 'px'}">
  10. <view class="uni-swiper__dots-item " v-for="(item,index) in info" :class="[index === current&&'uni-swiper__dots-long']"
  11. :style="{
  12. 'width':(index === current? dots.width*3:dots.width ) + 'px','height':dots.height +'px' ,'background-color':index !== current?dots.backgroundColor:dots.selectedBackgroundColor,'border':index !==current ? dots.border:dots.selectedBorder}"
  13. :key="index"></view>
  14. </view>
  15. <view v-if="mode === 'nav'" class="uni-swiper__dots-box uni-swiper__dots-nav" :style="{'background-color':dotsStyles.backgroundColor}">
  16. <view class="uni-swiper__dots-nav-item" :style="{'color':dotsStyles.color}">{{(current+1)+"/"+info.length}}
  17. {{info[current][field]}}</view>
  18. </view>
  19. <view v-if="mode === 'indexes'" class="uni-swiper__dots-box" :style="{'bottom':dots.bottom + 'px'}">
  20. <view class="uni-swiper__dots-item uni-swiper__dots-indexes" v-for="(item,index) in info" :style="{
  21. 'width':dots.width + 'px','height':dots.height +'px' ,'color':index === current?dots.selectedColor:dots.color,'background-color':index !== current?dots.backgroundColor:dots.selectedBackgroundColor,'border':index !==current ? dots.border:dots.selectedBorder}"
  22. :key="index">{{index+1}}</view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. export default {
  28. name: 'uni-swiper-dot',
  29. props: {
  30. info: {
  31. type: Array,
  32. default () {
  33. return []
  34. }
  35. },
  36. current: {
  37. info: Number,
  38. default: 0
  39. },
  40. dotsStyles: {
  41. type: Object,
  42. default () {
  43. return {}
  44. }
  45. },
  46. // 类型 :default(默认) indexes long nav
  47. mode: {
  48. type: String,
  49. default: 'default'
  50. },
  51. // 只在 nav 模式下生效,变量名称
  52. field: {
  53. type: String,
  54. default: ''
  55. }
  56. },
  57. data() {
  58. return {
  59. dots: {
  60. width: 8,
  61. height: 8,
  62. bottom: 10,
  63. color: '#fff',
  64. backgroundColor: 'rgba(0, 0, 0, .3)',
  65. border: '1px rgba(0, 0, 0, .3) solid',
  66. selectedBackgroundColor: '#333',
  67. selectedBorder: '1px rgba(0, 0, 0, .9) solid'
  68. }
  69. };
  70. },
  71. created() {
  72. if (this.mode === 'indexes') {
  73. this.dots.width = 20
  74. this.dots.height = 20
  75. }
  76. this.dots = Object.assign(this.dots, this.dotsStyles)
  77. },
  78. watch: {
  79. dotsStyles(newVal) {
  80. this.dots = Object.assign(this.dots, this.dotsStyles)
  81. },
  82. mode(newVal) {
  83. if (newVal === 'indexes') {
  84. this.dots.width = 20
  85. this.dots.height = 20
  86. } else {
  87. this.dots.width = 8
  88. this.dots.height = 8
  89. }
  90. }
  91. }
  92. }
  93. </script>
  94. <style>
  95. .uni-swiper__warp {
  96. position: relative;
  97. width: 100%;
  98. box-sizing: border-box;
  99. overflow: hidden;
  100. }
  101. .uni-swiper__dots-box {
  102. position: absolute;
  103. bottom: 20upx;
  104. display: flex;
  105. justify-content: center;
  106. align-items: center;
  107. box-sizing: box-sizing;
  108. width: 100%;
  109. }
  110. .uni-swiper__dots-item {
  111. flex-shrink: 0;
  112. width: 16upx;
  113. border-radius: 50%;
  114. margin-left: 12upx;
  115. background: rgba(0, 0, 0, .3);
  116. transition: all 0.2s linear;
  117. }
  118. .uni-swiper__dots-item:first-child {
  119. margin: 0;
  120. }
  121. .uni-swiper__dots-default {
  122. border-radius: 50%;
  123. }
  124. .uni-swiper__dots-long {
  125. border-radius: 100upx;
  126. }
  127. .uni-swiper__dots-nav {
  128. bottom: 0;
  129. height: 80upx;
  130. justify-content: flex-start;
  131. background: rgba(0, 0, 0, 0.2);
  132. box-sizing: box-sizing;
  133. overflow: hidden;
  134. }
  135. .uni-swiper__dots-nav-item {
  136. overflow: hidden;
  137. text-overflow: ellipsis;
  138. white-space: nowrap;
  139. font-size: 28upx;
  140. color: #fff;
  141. box-sizing: box-sizing;
  142. margin: 0 30upx;
  143. }
  144. .uni-swiper__dots-indexes {
  145. display: flex;
  146. justify-content: center;
  147. align-items: center;
  148. color: #fff;
  149. font-size: 24upx;
  150. }
  151. </style>