uni-swiper-dot.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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'}" :key="index" class="uni-swiper__dots-item uni-swiper__dots-bar" />
  7. </view>
  8. <view v-if="mode === 'dot'" :style="{'bottom':dots.bottom + 'px'}" class="uni-swiper__dots-box" key='dot'>
  9. <view v-for="(item,index) in info" :style="{
  10. '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" />
  11. </view>
  12. <view v-if="mode === 'round'" :style="{'bottom':dots.bottom + 'px'}" class="uni-swiper__dots-box" key='round'>
  13. <view v-for="(item,index) in info" :class="[index === current&&'uni-swiper__dots-long']" :style="{
  14. '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 " />
  15. </view>
  16. <view v-if="mode === 'nav'" key='nav' :style="{'background-color':dotsStyles.backgroundColor,'bottom':'0'}" class="uni-swiper__dots-box uni-swiper__dots-nav">
  17. <text :style="{'color':dotsStyles.color}" class="uni-swiper__dots-nav-item">{{ (current+1)+"/"+info.length +' ' +info[current][field] }}</text>
  18. </view>
  19. <view v-if="mode === 'indexes'" key='indexes' :style="{'bottom':dots.bottom + 'px'}" class="uni-swiper__dots-box">
  20. <view 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}" :key="index" class="uni-swiper__dots-item uni-swiper__dots-indexes"><text class="uni-swiper__dots-indexes-text">{{ index+1 }}</text></view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. /**
  27. * SwiperDod 轮播图指示点
  28. * @description 自定义轮播图指示点
  29. * @tutorial https://ext.dcloud.net.cn/plugin?id=284
  30. * @property {Number} current 当前指示点索引,必须是通过 `swiper` 的 `change` 事件获取到的 `e.detail.current`
  31. * @property {String} mode = [default|round|nav|indexes] 指示点的类型
  32. * @value defualt 默认指示点
  33. * @value round 圆形指示点
  34. * @value nav 条形指示点
  35. * @value indexes 索引指示点
  36. * @property {String} field mode 为 nav 时,显示的内容字段(mode = nav 时必填)
  37. * @property {String} info 轮播图的数据,通过数组长度决定指示点个数
  38. * @property {Object} dotsStyles 指示点样式
  39. * @event {Function} clickItem 组件触发点击事件时触发,e={currentIndex}
  40. */
  41. export default {
  42. name: 'UniSwiperDot',
  43. props: {
  44. info: {
  45. type: Array,
  46. default () {
  47. return []
  48. }
  49. },
  50. current: {
  51. type: Number,
  52. default: 0
  53. },
  54. dotsStyles: {
  55. type: Object,
  56. default () {
  57. return {}
  58. }
  59. },
  60. // 类型 :default(默认) indexes long nav
  61. mode: {
  62. type: String,
  63. default: 'default'
  64. },
  65. // 只在 nav 模式下生效,变量名称
  66. field: {
  67. type: String,
  68. default: ''
  69. }
  70. },
  71. data() {
  72. return {
  73. dots: {
  74. width: 8,
  75. height: 8,
  76. bottom: 10,
  77. color: '#fff',
  78. backgroundColor: 'rgba(0, 0, 0, .3)',
  79. border: '1px rgba(0, 0, 0, .3) solid',
  80. selectedBackgroundColor: '#333',
  81. selectedBorder: '1px rgba(0, 0, 0, .9) solid'
  82. }
  83. }
  84. },
  85. watch: {
  86. dotsStyles(newVal) {
  87. this.dots = Object.assign(this.dots, this.dotsStyles)
  88. },
  89. mode(newVal) {
  90. if (newVal === 'indexes') {
  91. this.dots.width = 20
  92. this.dots.height = 20
  93. } else {
  94. this.dots.width = 8
  95. this.dots.height = 8
  96. }
  97. }
  98. },
  99. created() {
  100. if (this.mode === 'indexes') {
  101. this.dots.width = 20
  102. this.dots.height = 20
  103. }
  104. this.dots = Object.assign(this.dots, this.dotsStyles)
  105. }
  106. }
  107. </script>
  108. <style scoped>
  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. position: absolute;
  120. bottom: 10px;
  121. left: 0;
  122. right: 0;
  123. /* #ifndef APP-NVUE */
  124. display: flex;
  125. /* #endif */
  126. flex: 1;
  127. flex-direction: row;
  128. justify-content: center;
  129. align-items: center;
  130. }
  131. .uni-swiper__dots-item {
  132. width: 8px;
  133. border-radius: 100px;
  134. margin-left: 6px;
  135. background-color: rgba(0, 0, 0, 0.4);
  136. /* transition: width 0.2s linear; 不要取消注释,不然会不能变色
  137. */
  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: 14px;
  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. */
  177. justify-content: center;
  178. align-items: center;
  179. }
  180. .uni-swiper__dots-indexes-text {
  181. color: #fff;
  182. font-size: 12px;
  183. }
  184. </style>