u-swiper.vue 11 KB

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