u-swiper.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <template>
  2. <view :style="{
  3. borderRadius: `${borderRadius}rpx`
  4. }" class="u-swiper-wrap">
  5. <swiper :autoplay="autoplay" :circular="circular" :current="elCurrent" :duration="duration"
  6. :interval="interval" :next-margin="effect3d ? effect3dPreviousMargin + 'rpx' : '0'" :previous-margin="effect3d ? effect3dPreviousMargin + 'rpx' : '0'"
  7. :style="{
  8. height: height + 'rpx',
  9. backgroundColor: bgColor
  10. }"
  11. @animationfinish="animationfinish"
  12. @change="change">
  13. <swiper-item v-for="(item, index) in list" :key="index" class="u-swiper-item">
  14. <view :class="[uCurrent != index ? 'u-list-scale' : '']" :style="{
  15. borderRadius: `${borderRadius}rpx`,
  16. transform: effect3d && uCurrent != index ? 'scaleY(0.9)' : 'scaleY(1)',
  17. margin: effect3d && uCurrent != index ? '0 20rpx' : 0,
  18. }"
  19. class="u-list-image-wrap" @tap.stop.prevent="listClick(index)">
  20. <image :mode="imgMode" :src="item[name] || item" class="u-swiper-image"></image>
  21. <view v-if="title && item.title" :style="[{
  22. 'padding-bottom': titlePaddingBottom
  23. }, titleStyle]" class="u-swiper-title u-line-1">
  24. {{ item.title }}
  25. </view>
  26. </view>
  27. </swiper-item>
  28. </swiper>
  29. <view :style="{
  30. top: indicatorPos == 'topLeft' || indicatorPos == 'topCenter' || indicatorPos == 'topRight' ? '12rpx' : 'auto',
  31. bottom: indicatorPos == 'bottomLeft' || indicatorPos == 'bottomCenter' || indicatorPos == 'bottomRight' ? '12rpx' : 'auto',
  32. justifyContent: justifyContent,
  33. padding: `0 ${effect3d ? '74rpx' : '24rpx'}`
  34. }" class="u-swiper-indicator">
  35. <block v-if="mode == 'rect'">
  36. <view v-for="(item, index) in list" :key="index"
  37. :class="{ 'u-indicator-item-rect-active': index == uCurrent }"
  38. class="u-indicator-item-rect"></view>
  39. </block>
  40. <block v-if="mode == 'dot'">
  41. <view v-for="(item, index) in list" :key="index"
  42. :class="{ 'u-indicator-item-dot-active': index == uCurrent }"
  43. class="u-indicator-item-dot"></view>
  44. </block>
  45. <block v-if="mode == 'round'">
  46. <view v-for="(item, index) in list" :key="index"
  47. :class="{ 'u-indicator-item-round-active': index == uCurrent }"
  48. class="u-indicator-item-round"></view>
  49. </block>
  50. <block v-if="mode == 'number'">
  51. <view class="u-indicator-item-number">{{ uCurrent + 1 }}/{{ list.length }}</view>
  52. </block>
  53. </view>
  54. </view>
  55. </template>
  56. <script>
  57. /**
  58. * swiper 轮播图
  59. * @description 该组件一般用于导航轮播,广告展示等场景,可开箱即用
  60. * @tutorial https://www.uviewui.com/components/swiper.html
  61. * @property {Array} list 轮播图数据,见官网"基本使用"说明
  62. * @property {Boolean} title 是否显示标题文字,需要配合list参数,见官网说明(默认false)
  63. * @property {String} mode 指示器模式,见官网说明(默认round)
  64. * @property {String Number} height 轮播图组件高度,单位rpx(默认250)
  65. * @property {String} indicator-pos 指示器的位置(默认bottomCenter)
  66. * @property {Boolean} effect3d 是否开启3D效果(默认false)
  67. * @property {Boolean} autoplay 是否自动播放(默认true)
  68. * @property {String Number} interval 自动轮播时间间隔,单位ms(默认2500)
  69. * @property {Boolean} circular 是否衔接播放,见官网说明(默认true)
  70. * @property {String} bg-color 背景颜色(默认#f3f4f6)
  71. * @property {String Number} border-radius 轮播图圆角值,单位rpx(默认8)
  72. * @property {Object} title-style 自定义标题样式
  73. * @property {String Number} effect3d-previous-margin mode = true模式的情况下,激活项与前后项之间的距离,单位rpx(默认50)
  74. * @property {String} img-mode 图片的裁剪模式,详见image组件裁剪模式(默认aspectFill)
  75. * @event {Function} click 点击轮播图时触发
  76. * @example <u-swiper :list="list" mode="dot" indicator-pos="bottomRight"></u-swiper>
  77. */
  78. export default {
  79. name: "u-swiper",
  80. props: {
  81. // 轮播图的数据,格式如:[{image: 'xxxx', title: 'xxxx'},{image: 'yyyy', title: 'yyyy'}],其中title字段可选
  82. list: {
  83. type: Array,
  84. default() {
  85. return [];
  86. }
  87. },
  88. // 是否显示title标题
  89. title: {
  90. type: Boolean,
  91. default: false
  92. },
  93. // 用户自定义的指示器的样式
  94. indicator: {
  95. type: Object,
  96. default() {
  97. return {};
  98. }
  99. },
  100. // 圆角值
  101. borderRadius: {
  102. type: [Number, String],
  103. default: 8
  104. },
  105. // 隔多久自动切换
  106. interval: {
  107. type: [String, Number],
  108. default: 3000
  109. },
  110. // 指示器的模式,rect|dot|number|round
  111. mode: {
  112. type: String,
  113. default: 'round'
  114. },
  115. // list的高度,单位rpx
  116. height: {
  117. type: [Number, String],
  118. default: 250
  119. },
  120. // 指示器的位置,topLeft|topCenter|topRight|bottomLeft|bottomCenter|bottomRight
  121. indicatorPos: {
  122. type: String,
  123. default: 'bottomCenter'
  124. },
  125. // 是否开启缩放效果
  126. effect3d: {
  127. type: Boolean,
  128. default: false
  129. },
  130. // 3D模式的情况下,激活item与前后item之间的距离,单位rpx
  131. effect3dPreviousMargin: {
  132. type: [Number, String],
  133. default: 50
  134. },
  135. // 是否自动播放
  136. autoplay: {
  137. type: Boolean,
  138. default: true
  139. },
  140. // 自动轮播时间间隔,单位ms
  141. duration: {
  142. type: [Number, String],
  143. default: 500
  144. },
  145. // 是否衔接滑动,即到最后一张时接着滑动,是否自动切换到第一张
  146. circular: {
  147. type: Boolean,
  148. default: true
  149. },
  150. // 图片的裁剪模式
  151. imgMode: {
  152. type: String,
  153. default: 'aspectFill'
  154. },
  155. // 从list数组中读取的图片的属性名
  156. name: {
  157. type: String,
  158. default: 'image'
  159. },
  160. // 背景颜色
  161. bgColor: {
  162. type: String,
  163. default: '#f3f4f6'
  164. },
  165. // 初始化时,默认显示第几项
  166. current: {
  167. type: [Number, String],
  168. default: 0
  169. },
  170. // 标题的样式,对象形式
  171. titleStyle: {
  172. type: Object,
  173. default() {
  174. return {}
  175. }
  176. }
  177. },
  178. watch: {
  179. // 如果外部的list发生变化,判断长度是否被修改,如果前后长度不一致,重置uCurrent值,避免溢出
  180. list(nVal, oVal) {
  181. if (nVal.length !== oVal.length) this.uCurrent = 0;
  182. },
  183. // 监听外部current的变化,实时修改内部依赖于此测uCurrent值,如果更新了current,而不是更新uCurrent,
  184. // 就会错乱,因为指示器是依赖于uCurrent的
  185. current(n) {
  186. this.uCurrent = n;
  187. }
  188. },
  189. data() {
  190. return {
  191. uCurrent: this.current // 当前活跃的swiper-item的index
  192. };
  193. },
  194. computed: {
  195. justifyContent() {
  196. if (this.indicatorPos == 'topLeft' || this.indicatorPos == 'bottomLeft') return 'flex-start';
  197. if (this.indicatorPos == 'topCenter' || this.indicatorPos == 'bottomCenter') return 'center';
  198. if (this.indicatorPos == 'topRight' || this.indicatorPos == 'bottomRight') return 'flex-end';
  199. },
  200. titlePaddingBottom() {
  201. let tmp = 0;
  202. if (this.mode == 'none') return '12rpx';
  203. if (['bottomLeft', 'bottomCenter', 'bottomRight'].indexOf(this.indicatorPos) >= 0 && this.mode == 'number') {
  204. tmp = '60rpx';
  205. } else if (['bottomLeft', 'bottomCenter', 'bottomRight'].indexOf(this.indicatorPos) >= 0 && this.mode != 'number') {
  206. tmp = '40rpx';
  207. } else {
  208. tmp = '12rpx';
  209. }
  210. return tmp;
  211. },
  212. // 因为uni的swiper组件的current参数只接受Number类型,这里做一个转换
  213. elCurrent() {
  214. return Number(this.current);
  215. }
  216. },
  217. methods: {
  218. listClick(index) {
  219. this.$emit('click', index);
  220. },
  221. change(e) {
  222. let current = e.detail.current;
  223. this.uCurrent = current;
  224. // 发出change事件,表示当前自动切换的index,从0开始
  225. this.$emit('change', current);
  226. },
  227. // 头条小程序不支持animationfinish事件,改由change事件
  228. // 暂不监听此事件,因为不再给swiper绑定uCurrent属性
  229. animationfinish(e) {
  230. // #ifndef MP-TOUTIAO
  231. // this.uCurrent = e.detail.current;
  232. // #endif
  233. }
  234. }
  235. };
  236. </script>
  237. <style lang="scss" scoped>
  238. @import "../../libs/css/style.components.scss";
  239. .u-swiper-wrap {
  240. position: relative;
  241. overflow: hidden;
  242. transform: translateY(0);
  243. }
  244. .u-swiper-image {
  245. width: 100%;
  246. will-change: transform;
  247. height: 100%;
  248. /* #ifndef APP-NVUE */
  249. display: block;
  250. /* #endif */
  251. /* #ifdef H5 */
  252. pointer-events: none;
  253. /* #endif */
  254. }
  255. .u-swiper-indicator {
  256. padding: 0 24 rpx;
  257. position: absolute;
  258. @include vue-flex;
  259. width: 100%;
  260. z-index: 1;
  261. }
  262. .u-indicator-item-rect {
  263. width: 26 rpx;
  264. height: 8 rpx;
  265. margin: 0 6 rpx;
  266. transition: all 0.5s;
  267. background-color: rgba(0, 0, 0, 0.3);
  268. }
  269. .u-indicator-item-rect-active {
  270. background-color: rgba(255, 255, 255, 0.8);
  271. }
  272. .u-indicator-item-dot {
  273. width: 14 rpx;
  274. height: 14 rpx;
  275. margin: 0 6 rpx;
  276. border-radius: 20 rpx;
  277. transition: all 0.5s;
  278. background-color: rgba(0, 0, 0, 0.3);
  279. }
  280. .u-indicator-item-dot-active {
  281. background-color: rgba(255, 255, 255, 0.8);
  282. }
  283. .u-indicator-item-round {
  284. width: 14 rpx;
  285. height: 14 rpx;
  286. margin: 0 6 rpx;
  287. border-radius: 20 rpx;
  288. transition: all 0.5s;
  289. background-color: rgba(0, 0, 0, 0.3);
  290. }
  291. .u-indicator-item-round-active {
  292. width: 34 rpx;
  293. background-color: rgba(255, 255, 255, 0.8);
  294. }
  295. .u-indicator-item-number {
  296. padding: 6 rpx 16 rpx;
  297. line-height: 1;
  298. background-color: rgba(0, 0, 0, 0.3);
  299. border-radius: 100 rpx;
  300. font-size: 26 rpx;
  301. color: rgba(255, 255, 255, 0.8);
  302. }
  303. .u-list-scale {
  304. transform-origin: center center;
  305. }
  306. .u-list-image-wrap {
  307. width: 100%;
  308. height: 100%;
  309. flex: 1;
  310. transition: all 0.5s;
  311. overflow: hidden;
  312. box-sizing: content-box;
  313. position: relative;
  314. }
  315. .u-swiper-title {
  316. position: absolute;
  317. background-color: rgba(0, 0, 0, 0.3);
  318. bottom: 0;
  319. left: 0;
  320. width: 100%;
  321. font-size: 28 rpx;
  322. padding: 12 rpx 24 rpx;
  323. color: rgba(255, 255, 255, 0.9);
  324. }
  325. .u-swiper-item {
  326. @include vue-flex;
  327. overflow: hidden;
  328. align-items: center;
  329. }
  330. </style>