u-swiper.vue 9.8 KB

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