cl-swiper.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <template>
  2. <view
  3. class="cl-swiper"
  4. :class="[`cl-swiper--${type}`, `cl-swiper--dot-${dotType}`]"
  5. :style="{ height }"
  6. @tap="onTap"
  7. >
  8. <swiper
  9. :indicator-dots="indicatorDots"
  10. :indicator-color="indicatorColor2"
  11. :indicator-active-color="indicatorActiveColor2"
  12. :active-class="activeClass"
  13. :changing-class="changingClass"
  14. :autoplay="autoplay"
  15. :current="current"
  16. :interval="interval"
  17. :duration="duration"
  18. :circular="circular2"
  19. :vertical="vertical"
  20. :previous-margin="previousMargin"
  21. :next-margin="nextMargin"
  22. :acceleration="acceleration"
  23. :disable-programmatic-animation="disableProgrammaticAnimation"
  24. :display-multiple-items="displayMultipleItems"
  25. :skip-hidden-item-layout="skipHiddenItemLayout"
  26. :disable-touch="disableTouch"
  27. :easing-function="easingFunction"
  28. @change="onChange"
  29. @transition="onTransition"
  30. @animationfinish="onAnimationfinish"
  31. >
  32. <swiper-item v-for="(item, index) in list" :key="index">
  33. <view
  34. class="cl-swiper-item"
  35. :class="[
  36. {
  37. 'is-active': current === index
  38. }
  39. ]"
  40. >
  41. <slot :item="item" :index="index">
  42. <image
  43. class="cl-swiper-item__image"
  44. :style="{
  45. 'border-radius': radius
  46. }"
  47. :mode="imageMode"
  48. :src="item.url"
  49. />
  50. </slot>
  51. </view>
  52. </swiper-item>
  53. </swiper>
  54. <view class="cl-swiper__btn" v-if="arrow">
  55. <button class="cl-swiper__btn-prev" @tap="onPrev" v-if="isPrev">
  56. <i class="cl-icon-arrow-left"></i>
  57. </button>
  58. <button class="cl-swiper__btn-next" @tap="onNext" v-if="isNext">
  59. <i class="cl-icon-arrow-right"></i>
  60. </button>
  61. </view>
  62. </view>
  63. </template>
  64. <script>
  65. /**
  66. * swiper 滑块视图,轮播图
  67. * @description 该组件基于官方的 swiper, 参数与官方一致, 同时添加新的支持
  68. * @tutorial https://docs.cool-js.com/uni/components/view/swiper.html
  69. * @property {Number} value 绑定值
  70. * @property {String} type 类型 '' | chain | card
  71. * @property {Boolean} arrow 是否显示切换箭头
  72. * @property {String} imageMode 图片裁剪, 缩放模式
  73. * @property {String} height 高度,默认300rpx
  74. * @property {String} radius 圆角,默认10rpx
  75. * @property {Array} margin 外间距
  76. * @property {String} dotType 指示器类型 round | circle | square
  77. * @event {Function} change 绑定值改变时触发
  78. * @example <cl-swiper v-model="active" :list="list"></cl-slider>
  79. */
  80. import { isString } from "../../utils";
  81. export default {
  82. name: "cl-swiper",
  83. props: {
  84. // 绑定值
  85. value: Number,
  86. // 类型 '' | chain | card
  87. type: String,
  88. // 是否显示切换箭头
  89. arrow: Boolean,
  90. // 数据列表
  91. list: Array,
  92. // 图片裁剪, 缩放模式
  93. imageMode: {
  94. type: String,
  95. default: "aspectFill"
  96. },
  97. // 高度
  98. height: {
  99. type: String,
  100. default: "300rpx"
  101. },
  102. // 圆角
  103. radius: {
  104. type: String,
  105. default: "10rpx"
  106. },
  107. // 外间距
  108. margin: {
  109. type: Array,
  110. default: () => []
  111. },
  112. // 是否显示指示器
  113. indicatorDots: Boolean,
  114. // 指示器颜色
  115. indicatorColor: {
  116. type: String,
  117. default: "rgba(0, 0, 0, .3)"
  118. },
  119. // 指示器选中颜色
  120. indicatorActiveColor: {
  121. type: String,
  122. default: "#000000"
  123. },
  124. // 指示器类型 round | circle | square
  125. dotType: {
  126. type: String,
  127. default: "round",
  128. validator: val => {
  129. return ["round", "circle", "square"].indexOf(val) !== -1;
  130. }
  131. },
  132. activeClass: String,
  133. changingClass: String,
  134. autoplay: Boolean,
  135. currentItemId: String,
  136. interval: {
  137. type: Number,
  138. default: 5000
  139. },
  140. duration: {
  141. type: Number,
  142. default: 500
  143. },
  144. circular: Boolean,
  145. vertical: Boolean,
  146. acceleration: Boolean,
  147. disableProgrammaticAnimation: Boolean,
  148. displayMultipleItems: {
  149. type: Number,
  150. default: 1
  151. },
  152. skipHiddenItemLayout: Boolean,
  153. disableTouch: Boolean,
  154. touchable: {
  155. type: Boolean,
  156. default: true
  157. },
  158. easingFunction: {
  159. type: String,
  160. default: "default"
  161. }
  162. },
  163. data() {
  164. return {
  165. current: 0
  166. };
  167. },
  168. watch: {
  169. value: {
  170. immediate: true,
  171. handler(val) {
  172. this.current = val || 0;
  173. }
  174. },
  175. current(val) {
  176. this.$emit("input", val);
  177. this.$emit("change", val);
  178. }
  179. },
  180. computed: {
  181. previousMargin() {
  182. let [a, b, c] = this.margin;
  183. switch (this.type) {
  184. case "chain":
  185. switch (this.current) {
  186. case 0:
  187. return a || "0";
  188. case this.list.length - 1:
  189. return c || "120rpx";
  190. default:
  191. return b || "70rpx";
  192. }
  193. case "card":
  194. return a || "80rpx";
  195. default:
  196. return a || "0";
  197. }
  198. },
  199. nextMargin() {
  200. let [a, b, c] = this.margin;
  201. switch (this.type) {
  202. case "chain":
  203. switch (this.current) {
  204. case 0:
  205. return c || "120rpx";
  206. case this.list.length - 1:
  207. return a || "0";
  208. default:
  209. return b || "70rpx";
  210. }
  211. case "card":
  212. return b || "80rpx";
  213. default:
  214. return b || "0";
  215. }
  216. },
  217. circular2() {
  218. switch (this.type) {
  219. case "card":
  220. return true;
  221. default:
  222. return this.circular;
  223. }
  224. },
  225. indicatorColor2() {
  226. return this.dotType ? "" : this.indicatorColor;
  227. },
  228. indicatorActiveColor2() {
  229. return this.dotType ? "" : this.indicatorActiveColor;
  230. },
  231. isPrev() {
  232. return this.current > 0;
  233. },
  234. isNext() {
  235. return this.current < this.list.length - 1;
  236. },
  237. flist() {
  238. return this.list.map(e => {
  239. if (isString(e)) {
  240. return {
  241. url: e
  242. };
  243. } else {
  244. return e;
  245. }
  246. });
  247. }
  248. },
  249. methods: {
  250. onChange(e) {
  251. this.current = e.detail.current;
  252. },
  253. onTransition(e) {
  254. this.$emit("transition", e);
  255. },
  256. onAnimationfinish(e) {
  257. this.$emit("animationfinish", e);
  258. },
  259. onPrev() {
  260. this.current -= this.isPrev ? 1 : 0;
  261. },
  262. onNext() {
  263. this.current += this.isNext ? 1 : 0;
  264. },
  265. onTap() {
  266. this.$emit("click", this.current);
  267. this.$emit("tap", this.current);
  268. }
  269. }
  270. };
  271. </script>