uni-swiper-dot.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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" @click="clickItem(index)" :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" @click="clickItem(index)" :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" @click="clickItem(index)" :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 + 'px'}" class="uni-swiper__dots-box">
  23. <view v-for="(item,index) in info" @click="clickItem(index)" :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>
  28. </template>
  29. <script>
  30. /**
  31. * SwiperDod 轮播图指示点
  32. * @description 自定义轮播图指示点
  33. * @tutorial https://ext.dcloud.net.cn/plugin?id=284
  34. * @property {Number} current 当前指示点索引,必须是通过 `swiper` 的 `change` 事件获取到的 `e.detail.current`
  35. * @property {String} mode = [default|round|nav|indexes] 指示点的类型
  36. * @value defualt 默认指示点
  37. * @value round 圆形指示点
  38. * @value nav 条形指示点
  39. * @value indexes 索引指示点
  40. * @property {String} field mode 为 nav 时,显示的内容字段(mode = nav 时必填)
  41. * @property {String} info 轮播图的数据,通过数组长度决定指示点个数
  42. * @property {Object} dotsStyles 指示点样式
  43. * @event {Function} clickItem 组件触发点击事件时触发,e={currentIndex}
  44. */
  45. export default {
  46. name: 'UniSwiperDot',
  47. props: {
  48. info: {
  49. type: Array,
  50. default () {
  51. return []
  52. }
  53. },
  54. current: {
  55. type: Number,
  56. default: 0
  57. },
  58. dotsStyles: {
  59. type: Object,
  60. default () {
  61. return {}
  62. }
  63. },
  64. // 类型 :default(默认) indexes long nav
  65. mode: {
  66. type: String,
  67. default: 'default'
  68. },
  69. // 只在 nav 模式下生效,变量名称
  70. field: {
  71. type: String,
  72. default: ''
  73. }
  74. },
  75. data() {
  76. return {
  77. dots: {
  78. width: 8,
  79. height: 8,
  80. bottom: 10,
  81. color: '#fff',
  82. backgroundColor: 'rgba(0, 0, 0, .3)',
  83. border: '1px rgba(0, 0, 0, .3) solid',
  84. selectedBackgroundColor: '#333',
  85. selectedBorder: '1px rgba(0, 0, 0, .9) solid'
  86. }
  87. }
  88. },
  89. watch: {
  90. dotsStyles(newVal) {
  91. this.dots = Object.assign(this.dots, this.dotsStyles)
  92. },
  93. mode(newVal) {
  94. if (newVal === 'indexes') {
  95. this.dots.width = 20
  96. this.dots.height = 20
  97. } else {
  98. this.dots.width = 8
  99. this.dots.height = 8
  100. }
  101. }
  102. },
  103. created() {
  104. if (this.mode === 'indexes') {
  105. this.dots.width = 20
  106. this.dots.height = 20
  107. }
  108. this.dots = Object.assign(this.dots, this.dotsStyles)
  109. },
  110. methods: {
  111. clickItem(index) {
  112. this.$emit('clickItem', index)
  113. }
  114. }
  115. }
  116. </script>
  117. <style lang="scss" scoped>
  118. .uni-swiper__warp {
  119. /* #ifndef APP-NVUE */
  120. display: flex;
  121. /* #endif */
  122. flex: 1;
  123. flex-direction: column;
  124. position: relative;
  125. overflow: hidden;
  126. }
  127. .uni-swiper__dots-box {
  128. position: absolute;
  129. bottom: 10px;
  130. left: 0;
  131. right: 0;
  132. /* #ifndef APP-NVUE */
  133. display: flex;
  134. /* #endif */
  135. flex: 1;
  136. flex-direction: row;
  137. justify-content: center;
  138. align-items: center;
  139. }
  140. .uni-swiper__dots-item {
  141. width: 8px;
  142. border-radius: 100px;
  143. margin-left: 6px;
  144. background-color: $uni-bg-color-mask;
  145. /* #ifndef APP-NVUE */
  146. cursor: pointer;
  147. /* #endif */
  148. /* #ifdef H5 */
  149. // border-width: 5px 0;
  150. // border-style: solid;
  151. // border-color: transparent;
  152. // background-clip: padding-box;
  153. /* #endif */
  154. // transition: width 0.2s linear; 不要取消注释,不然会不能变色
  155. }
  156. .uni-swiper__dots-item:first-child {
  157. margin: 0;
  158. }
  159. .uni-swiper__dots-default {
  160. border-radius: 100px;
  161. }
  162. .uni-swiper__dots-long {
  163. border-radius: 50px;
  164. }
  165. .uni-swiper__dots-bar {
  166. border-radius: 50px;
  167. }
  168. .uni-swiper__dots-nav {
  169. bottom: 0px;
  170. height: 40px;
  171. /* #ifndef APP-NVUE */
  172. display: flex;
  173. /* #endif */
  174. flex: 1;
  175. flex-direction: row;
  176. justify-content: flex-start;
  177. align-items: center;
  178. background-color: rgba(0, 0, 0, 0.2);
  179. }
  180. .uni-swiper__dots-nav-item {
  181. /* overflow: hidden;
  182. text-overflow: ellipsis;
  183. white-space: nowrap; */
  184. font-size: $uni-font-size-base;
  185. color: #fff;
  186. margin: 0 15px;
  187. }
  188. .uni-swiper__dots-indexes {
  189. /* #ifndef APP-NVUE */
  190. display: flex;
  191. /* #endif */
  192. // flex: 1;
  193. justify-content: center;
  194. align-items: center;
  195. }
  196. .uni-swiper__dots-indexes-text {
  197. color: #fff;
  198. font-size: $uni-font-size-sm;
  199. }
  200. </style>