uni-swiper-dot.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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" 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 + 'px'}" 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 + 'px'}" 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'&&showControls" 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. <text :style="{'color':dotsStyles.color}" class="uni-swiper__dots-nav-item">{{ (current+1)+"/"+info.length +' '}}</text>
  22. </view>
  23. <view v-if="mode === 'indexes'" key='indexes' :style="{'bottom':dots.bottom + 'px'}" class="uni-swiper__dots-box">
  24. <view v-for="(item,index) in info" :style="{
  25. '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}"
  26. :key="index" class="uni-swiper__dots-item uni-swiper__dots-indexes"><text class="uni-swiper__dots-indexes-text">{{ index+1 }}</text></view>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. export default {
  32. name: 'UniSwiperDot',
  33. props: {
  34. info: {
  35. type: Array,
  36. default () {
  37. return []
  38. }
  39. },
  40. current: {
  41. type: Number,
  42. default: 0
  43. },
  44. dotsStyles: {
  45. type: Object,
  46. default () {
  47. return {}
  48. }
  49. },
  50. showControls:{
  51. type: Boolean,
  52. default: true
  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>
  103. .uni-swiper__warp {
  104. /* #ifndef APP-NVUE */
  105. display: flex;
  106. /* #endif */
  107. flex: 1;
  108. flex-direction: column;
  109. position: relative;
  110. overflow: hidden;
  111. }
  112. .uni-swiper__dots-box {
  113. position: absolute;
  114. bottom: 10px;
  115. left: 0;
  116. right: 0;
  117. /* #ifndef APP-NVUE */
  118. display: flex;
  119. /* #endif */
  120. flex: 1;
  121. flex-direction: row;
  122. justify-content: center;
  123. align-items: center;
  124. }
  125. .uni-swiper__dots-item {
  126. width: 8px;
  127. border-radius: 100px;
  128. margin-left: 6px;
  129. background-color: $uni-bg-color-mask;
  130. // transition: width 0.2s linear; 不要取消注释,不然会不能变色
  131. }
  132. .uni-swiper__dots-item:first-child {
  133. margin: 0;
  134. }
  135. .uni-swiper__dots-default {
  136. border-radius: 100px;
  137. }
  138. .uni-swiper__dots-long {
  139. border-radius: 50px;
  140. }
  141. .uni-swiper__dots-bar {
  142. border-radius: 50px;
  143. }
  144. .uni-swiper__dots-nav {
  145. bottom: 0px;
  146. height: 40px;
  147. /* #ifndef APP-NVUE */
  148. display: flex;
  149. /* #endif */
  150. flex: 1;
  151. flex-direction: row;
  152. /* justify-content: flex-start; */
  153. align-items: center;
  154. /* background-color: rgba(0, 0, 0, 0.2); */
  155. justify-content: flex-end;
  156. }
  157. .uni-swiper__dots-nav-item {
  158. /* overflow: hidden;
  159. text-overflow: ellipsis;
  160. white-space: nowrap; */
  161. font-size: $uni-font-size-base;
  162. color: #fff;
  163. margin: 0 15px;
  164. bottom: 31rpx;
  165. right: 29rpx;
  166. width: 60rpx;
  167. height: 32rpx;
  168. background: rgba(0,0,0,0.2);
  169. border-radius: 16rpx;
  170. font-size: 24rpx;
  171. font-weight: 500;
  172. color: rgba(255,255,255,1);
  173. z-index: 10;
  174. text-align: center;
  175. line-height: 32rpx;
  176. }
  177. .uni-swiper__dots-indexes {
  178. /* #ifndef APP-NVUE */
  179. display: flex;
  180. /* #endif */
  181. // flex: 1;
  182. justify-content: center;
  183. align-items: center;
  184. }
  185. .uni-swiper__dots-indexes-text {
  186. color: #fff;
  187. font-size: $uni-font-size-sm;
  188. }
  189. </style>