uni-swiper-dot.vue 4.1 KB

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