u-swiper.vue 9.6 KB

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