uni-swiper-dot.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <template>
  2. <view class="uni-swiper__warp">
  3. <slot />
  4. <view v-if="mode === 'default'" :style="{'bottom':dots.bottom}" class="uni-swiper__dots-box" key='default'>
  5. <view v-for="(item,index) in info" :style="{
  6. 'width': (index === current? dots.width*2:dots.width ) + 'px','height':dots.width/3 +'px' ,'background-color':index !== current?dots.backgroundColor:dots.selectedBackgroundColor,'border-radius':'0px'}"
  7. :key="index" class="uni-swiper__dots-item uni-swiper__dots-bar" />
  8. </view>
  9. <view v-if="mode === 'dot'" :style="{'bottom':dots.bottom}" class="uni-swiper__dots-box" key='dot'>
  10. <view v-for="(item,index) in info" :style="{
  11. 'width': dots.width + 'px','height':dots.height +'px' ,'background-color':index !== current?dots.backgroundColor:dots.selectedBackgroundColor,'border':index !==current ? dots.border:dots.selectedBorder}"
  12. :key="index" class="uni-swiper__dots-item" />
  13. </view>
  14. <view v-if="mode === 'round'" :style="{'bottom':dots.bottom}" class="uni-swiper__dots-box" key='round'>
  15. <view v-for="(item,index) in info" :class="[index === current&&'uni-swiper__dots-long']" :style="{
  16. '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}"
  17. :key="index" class="uni-swiper__dots-item " />
  18. </view>
  19. <view v-if="mode === 'nav'" key='nav' :style="{'background-color':dotsStyles.backgroundColor,'bottom':'0'}" class="uni-swiper__dots-box uni-swiper__dots-nav">
  20. <text :style="{'color':dotsStyles.color}" class="uni-swiper__dots-nav-item">{{ (current+1)+"/"+info.length +' ' +info[current][field] }}</text>
  21. </view>
  22. <view v-if="mode === 'indexes'" key='indexes' :style="{'bottom':dots.bottom}" class="uni-swiper__dots-box">
  23. <view v-for="(item,index) in info" :style="{
  24. '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}"
  25. :key="index" class="uni-swiper__dots-item uni-swiper__dots-indexes"><text class="uni-swiper__dots-indexes-text">{{ index+1 }}</text></view>
  26. </view>
  27. <view v-if="mode === 'customize' &&info.length > 1" key='indexes' class="uni-swiper__dots-box customize">
  28. <view v-for="(item,index) in info" :style="{
  29. 'width':index === current?'20rpx':'16rpx','height': '6rpx' ,'background-color':index === current?'#ff4544':'#e2e2e2'}"
  30. :key="index" class="uni-swiper__dots-item uni-swiper__dots-indexes"></view>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. export default {
  36. name: 'UniSwiperDot',
  37. props: {
  38. info: {
  39. type: Array,
  40. default () {
  41. return []
  42. }
  43. },
  44. current: {
  45. type: Number,
  46. default: 0
  47. },
  48. dotsStyles: {
  49. type: Object,
  50. default () {
  51. return {}
  52. }
  53. },
  54. // 类型 :default(默认) indexes long nav
  55. mode: {
  56. type: String,
  57. default: 'default'
  58. },
  59. // 只在 nav 模式下生效,变量名称
  60. field: {
  61. type: String,
  62. default: ''
  63. }
  64. },
  65. data() {
  66. return {
  67. dots: {
  68. width: 8,
  69. height: 8,
  70. bottom: 10,
  71. color: '#fff',
  72. backgroundColor: 'rgba(0, 0, 0, .3)',
  73. border: '1px rgba(0, 0, 0, .3) solid',
  74. selectedBackgroundColor: '#333',
  75. selectedBorder: '1px rgba(0, 0, 0, .9) solid'
  76. }
  77. }
  78. },
  79. watch: {
  80. dotsStyles(newVal) {
  81. this.dots = Object.assign(this.dots, this.dotsStyles)
  82. },
  83. mode(newVal) {
  84. if (newVal === 'indexes') {
  85. this.dots.width = 20
  86. this.dots.height = 20
  87. } else {
  88. this.dots.width = 8
  89. this.dots.height = 8
  90. }
  91. }
  92. },
  93. created() {
  94. if (this.mode === 'indexes') {
  95. this.dots.width = 20
  96. this.dots.height = 20
  97. }
  98. this.dots = Object.assign(this.dots, this.dotsStyles)
  99. }
  100. }
  101. </script>
  102. <style lang="scss" scoped>
  103. .customize {
  104. bottom: #{24rpx};
  105. .uni-swiper__dots-item {
  106. margin-left: #{4rpx};
  107. border-radius: #{6rpx};
  108. }
  109. }
  110. .uni-swiper__warp {
  111. /* #ifndef APP-NVUE */
  112. display: flex;
  113. /* #endif */
  114. flex: 1;
  115. flex-direction: column;
  116. position: relative;
  117. overflow: hidden;
  118. }
  119. .uni-swiper__dots-box {
  120. position: absolute;
  121. bottom: 10px;
  122. left: 0;
  123. right: 0;
  124. /* #ifndef APP-NVUE */
  125. display: flex;
  126. /* #endif */
  127. flex: 1;
  128. flex-direction: row;
  129. justify-content: center;
  130. align-items: center;
  131. }
  132. .uni-swiper__dots-item {
  133. width: 8px;
  134. border-radius: 100px;
  135. margin-left: 6px;
  136. background-color: $uni-bg-color-mask;
  137. // transition: width 0.2s linear; 不要取消注释,不然会不能变色
  138. }
  139. .uni-swiper__dots-item:first-child {
  140. margin: 0;
  141. }
  142. .uni-swiper__dots-default {
  143. border-radius: 100px;
  144. }
  145. .uni-swiper__dots-long {
  146. border-radius: 50px;
  147. }
  148. .uni-swiper__dots-bar {
  149. border-radius: 50px;
  150. }
  151. .uni-swiper__dots-nav {
  152. bottom: 0px;
  153. height: 40px;
  154. /* #ifndef APP-NVUE */
  155. display: flex;
  156. /* #endif */
  157. flex: 1;
  158. flex-direction: row;
  159. justify-content: flex-start;
  160. align-items: center;
  161. background-color: rgba(0, 0, 0, 0.2);
  162. }
  163. .uni-swiper__dots-nav-item {
  164. /* overflow: hidden;
  165. text-overflow: ellipsis;
  166. white-space: nowrap; */
  167. font-size: $uni-font-size-base;
  168. color: #fff;
  169. margin: 0 15px;
  170. }
  171. .uni-swiper__dots-indexes {
  172. /* #ifndef APP-NVUE */
  173. display: flex;
  174. /* #endif */
  175. // flex: 1;
  176. justify-content: center;
  177. align-items: center;
  178. }
  179. .uni-swiper__dots-indexes-text {
  180. color: #fff;
  181. font-size: $uni-font-size-sm;
  182. }
  183. </style>