uni-swiper-dot.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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/2 +'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. emits:['clickItem'],
  48. props: {
  49. info: {
  50. type: Array,
  51. default () {
  52. return []
  53. }
  54. },
  55. current: {
  56. type: Number,
  57. default: 0
  58. },
  59. dotsStyles: {
  60. type: Object,
  61. default () {
  62. return {}
  63. }
  64. },
  65. // 类型 :default(默认) indexes long nav
  66. mode: {
  67. type: String,
  68. default: 'default'
  69. },
  70. // 只在 nav 模式下生效,变量名称
  71. field: {
  72. type: String,
  73. default: ''
  74. }
  75. },
  76. data() {
  77. return {
  78. dots: {
  79. width: 6,
  80. height: 6,
  81. bottom: 10,
  82. color: '#fff',
  83. backgroundColor: 'rgba(0, 0, 0, .3)',
  84. border: '1px rgba(0, 0, 0, .3) solid',
  85. selectedBackgroundColor: '#333',
  86. selectedBorder: '1px rgba(0, 0, 0, .9) solid'
  87. }
  88. }
  89. },
  90. watch: {
  91. dotsStyles(newVal) {
  92. this.dots = Object.assign(this.dots, this.dotsStyles)
  93. },
  94. mode(newVal) {
  95. if (newVal === 'indexes') {
  96. this.dots.width = 14
  97. this.dots.height = 14
  98. } else {
  99. this.dots.width = 6
  100. this.dots.height = 6
  101. }
  102. }
  103. },
  104. created() {
  105. if (this.mode === 'indexes') {
  106. this.dots.width = 12
  107. this.dots.height = 12
  108. }
  109. this.dots = Object.assign(this.dots, this.dotsStyles)
  110. },
  111. methods: {
  112. clickItem(index) {
  113. this.$emit('clickItem', index)
  114. }
  115. }
  116. }
  117. </script>
  118. <style lang="scss" scoped>
  119. .uni-swiper__warp {
  120. /* #ifndef APP-NVUE */
  121. display: flex;
  122. /* #endif */
  123. flex: 1;
  124. flex-direction: column;
  125. position: relative;
  126. overflow: hidden;
  127. }
  128. .uni-swiper__dots-box {
  129. position: absolute;
  130. bottom: 10px;
  131. left: 0;
  132. right: 0;
  133. /* #ifndef APP-NVUE */
  134. display: flex;
  135. /* #endif */
  136. flex: 1;
  137. flex-direction: row;
  138. justify-content: center;
  139. align-items: center;
  140. }
  141. .uni-swiper__dots-item {
  142. width: 8px;
  143. border-radius: 100px;
  144. margin-left: 6px;
  145. background-color: rgba(0, 0, 0, 0.4);
  146. /* #ifndef APP-NVUE */
  147. cursor: pointer;
  148. /* #endif */
  149. /* #ifdef H5 */
  150. // border-width: 5px 0;
  151. // border-style: solid;
  152. // border-color: transparent;
  153. // background-clip: padding-box;
  154. /* #endif */
  155. // transition: width 0.2s linear; 不要取消注释,不然会不能变色
  156. }
  157. .uni-swiper__dots-item:first-child {
  158. margin: 0;
  159. }
  160. .uni-swiper__dots-default {
  161. border-radius: 100px;
  162. }
  163. .uni-swiper__dots-long {
  164. border-radius: 50px;
  165. }
  166. .uni-swiper__dots-bar {
  167. border-radius: 50px;
  168. }
  169. .uni-swiper__dots-nav {
  170. bottom: 0px;
  171. // height: 26px;
  172. padding: 8px 0;
  173. /* #ifndef APP-NVUE */
  174. display: flex;
  175. /* #endif */
  176. flex: 1;
  177. flex-direction: row;
  178. justify-content: flex-start;
  179. align-items: center;
  180. background-color: rgba(0, 0, 0, 0.2);
  181. }
  182. .uni-swiper__dots-nav-item {
  183. /* overflow: hidden;
  184. text-overflow: ellipsis;
  185. white-space: nowrap; */
  186. font-size: 14px;
  187. color: #fff;
  188. margin: 0 15px;
  189. }
  190. .uni-swiper__dots-indexes {
  191. /* #ifndef APP-NVUE */
  192. display: flex;
  193. /* #endif */
  194. // flex: 1;
  195. justify-content: center;
  196. align-items: center;
  197. }
  198. .uni-swiper__dots-indexes-text {
  199. color: #fff;
  200. font-size: 12px;
  201. line-height: 14px;
  202. }
  203. </style>