u-swiper.vue 9.8 KB

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