uni-swiper-dot.vue 5.6 KB

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