u-swiper.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <view class="u-swiper" :style="{
  3. backgroundColor: bgColor,
  4. height: $u.addUnit(height),
  5. borderRadius: $u.addUnit(radius)
  6. }">
  7. <view class="u-swiper__loading" v-if="loading">
  8. <u-loading-icon mode="circle"></u-loading-icon>
  9. </view>
  10. <swiper v-else class="u-swiper__wrapper" :style="{
  11. height: $u.addUnit(height),
  12. }" @change="change" :circular="circular" :interval="interval" :duration="duration" :autoplay="autoplay"
  13. :current="current" :currentItemId="currentItemId" :previousMargin="$u.addUnit(previousMargin)"
  14. :nextMargin="$u.addUnit(nextMargin)" :acceleration="acceleration"
  15. :displayMultipleItems="displayMultipleItems" :easingFunction="easingFunction">
  16. <swiper-item class="u-swiper__wrapper__item" v-for="(item, index) in list" :key="index">
  17. <view class="u-swiper__wrapper__item__wrapper" :style="[itemStyle(index)]">
  18. <!-- 在nvue中,image图片的宽度默认为屏幕宽度,需要通过flex:1撑开,另外必须设置高度才能显示图片 -->
  19. <image class="u-swiper__wrapper__item__wrapper__image" v-if="getItemType(item) === 'image'"
  20. :src="getSource(item)" :mode="imgMode" @tap="clickHandler(index)" :style="{
  21. height: $u.addUnit(height),
  22. borderRadius: $u.addUnit(radius)
  23. }"></image>
  24. <video class="u-swiper__wrapper__item__wrapper__video" v-if="getItemType(item) === 'video'"
  25. :id="`video-${index}`" :enable-progress-gesture="false" :src="getSource(item)"
  26. :poster="getPoster(item)"
  27. :title="showTitle && $u.test.object(item) && item.title ? item.title : ''" :style="{
  28. height: $u.addUnit(height)
  29. }" controls @tap="clickHandler(index)"></video>
  30. <text v-if="showTitle && $u.test.object(item) && item.title && $u.test.image(getSource(item))"
  31. class="u-swiper__wrapper__item__wrapper__title u-line-1">{{ item.title }}</text>
  32. </view>
  33. </swiper-item>
  34. </swiper>
  35. <view class="u-swiper__indicator" :style="[$u.addStyle(indicatorStyle)]">
  36. <slot name="indicator">
  37. <u-swiper-indicator v-if="!loading && indicator && !showTitle"
  38. :indicatorActiveColor="indicatorActiveColor" :indicatorInactiveColor="indicatorInactiveColor"
  39. :length="list.length" :current="currentIndex" :indicatorMode="indicatorMode"></u-swiper-indicator>
  40. </slot>
  41. </view>
  42. </view>
  43. </template>
  44. <script>
  45. import props from './props.js';
  46. import mixin from '../../libs/mixin/mixin'
  47. import mpMixin from '../../libs/mixin/mpMixin';
  48. /**
  49. * Swiper 轮播图
  50. * @description 该组件一般用于导航轮播,广告展示等场景,可开箱即用,
  51. * @tutorial https://uview.d3u.cn/components/swiper.html
  52. * @property {Array} list 轮播图数据
  53. * @property {Boolean} indicator 是否显示面板指示器(默认 false )
  54. * @property {String} indicatorActiveColor 指示器非激活颜色(默认 '#FFFFFF' )
  55. * @property {String} indicatorInactiveColor 指示器的激活颜色(默认 'rgba(255, 255, 255, 0.35)' )
  56. * @property {String | Object} indicatorStyle 指示器样式,可通过bottom,left,right进行定位
  57. * @property {String} indicatorMode 指示器模式(默认 'line' )
  58. * @property {Boolean} autoplay 是否自动切换(默认 true )
  59. * @property {String | Number} current 当前所在滑块的 index(默认 0 )
  60. * @property {String} currentItemId 当前所在滑块的 item-id ,不能与 current 被同时指定
  61. * @property {String | Number} interval 滑块自动切换时间间隔(ms)(默认 3000 )
  62. * @property {String | Number} duration 滑块切换过程所需时间(ms)(默认 300 )
  63. * @property {Boolean} circular 播放到末尾后是否重新回到开头(默认 false )
  64. * @property {String | Number} previousMargin 前边距,可用于露出前一项的一小部分,nvue和支付宝不支持(默认 0 )
  65. * @property {String | Number} nextMargin 后边距,可用于露出后一项的一小部分,nvue和支付宝不支持(默认 0 )
  66. * @property {Boolean} acceleration 当开启时,会根据滑动速度,连续滑动多屏,支付宝不支持(默认 false )
  67. * @property {Number} displayMultipleItems 同时显示的滑块数量,nvue、支付宝小程序不支持(默认 1 )
  68. * @property {String} easingFunction 指定swiper切换缓动动画类型, 只对微信小程序有效(默认 'default' )
  69. * @property {String} keyName list数组中指定对象的目标属性名(默认 'url' )
  70. * @property {String} imgMode 图片的裁剪模式(默认 'aspectFill' )
  71. * @property {String | Number} height 组件高度(默认 130 )
  72. * @property {String} bgColor 背景颜色(默认 '#f3f4f6' )
  73. * @property {String | Number} radius 组件圆角,数值或带单位的字符串(默认 4 )
  74. * @property {Boolean} loading 是否加载中(默认 false )
  75. * @property {Boolean} showTitle 是否显示标题,要求数组对象中有title属性(默认 false )
  76. * @event {Function(index)} click 点击轮播图时触发 index:点击了第几张图片,从0开始
  77. * @event {Function(index)} change 轮播图切换时触发(自动或者手动切换) index:切换到了第几张图片,从0开始
  78. * @example <u-swiper :list="list4" keyName="url" :autoplay="false"></u-swiper>
  79. */
  80. export default {
  81. name: 'u-swiper',
  82. mixins: [mpMixin, mixin, props],
  83. data() {
  84. return {
  85. currentIndex: 0
  86. }
  87. },
  88. watch: {
  89. current(val, preVal) {
  90. if (val === preVal) return;
  91. this.currentIndex = val; // 和上游数据关联上
  92. }
  93. },
  94. computed: {
  95. itemStyle() {
  96. return index => {
  97. const style = {}
  98. // #ifndef APP-NVUE || MP-TOUTIAO
  99. // 左右流出空间的写法不支持nvue和头条
  100. // 只有配置了此二值,才加上对应的圆角,以及缩放
  101. if (this.nextMargin && this.previousMargin) {
  102. style.borderRadius = uni.$u.addUnit(this.radius)
  103. if (index !== this.currentIndex) style.transform = 'scale(0.92)'
  104. }
  105. // #endif
  106. return style
  107. }
  108. }
  109. },
  110. // #ifdef VUE3
  111. emits: ["click", "change"],
  112. // #endif
  113. methods: {
  114. getItemType(item) {
  115. if (typeof item === 'string') return uni.$u.test.video(this.getSource(item)) ? 'video' : 'image'
  116. if (typeof item === 'object' && this.keyName) {
  117. if (!item.type) return uni.$u.test.video(this.getSource(item)) ? 'video' : 'image'
  118. if (item.type === 'image') return 'image'
  119. if (item.type === 'video') return 'video'
  120. return 'image'
  121. }
  122. },
  123. // 获取目标路径,可能数组中为字符串,对象的形式,额外可指定对象的目标属性名keyName
  124. getSource(item) {
  125. if (typeof item === 'string') return item
  126. if (typeof item === 'object' && this.keyName) return item[this.keyName]
  127. else uni.$u.error('请按格式传递列表参数')
  128. return ''
  129. },
  130. // 轮播切换事件
  131. change(e) {
  132. // 当前的激活索引
  133. const {
  134. current
  135. } = e.detail
  136. this.pauseVideo(this.currentIndex)
  137. this.currentIndex = current
  138. this.$emit('change', e.detail)
  139. },
  140. // 切换轮播时,暂停视频播放
  141. pauseVideo(index) {
  142. const lastItem = this.getSource(this.list[index])
  143. if (uni.$u.test.video(lastItem)) {
  144. // 当视频隐藏时,暂停播放
  145. const video = uni.createVideoContext(`video-${index}`, this)
  146. video.pause()
  147. }
  148. },
  149. // 当一个轮播item为视频时,获取它的视频海报
  150. getPoster(item) {
  151. return typeof item === 'object' && item.poster ? item.poster : ''
  152. },
  153. // 点击某个item
  154. clickHandler(index) {
  155. this.$emit('click', index)
  156. }
  157. },
  158. }
  159. </script>
  160. <style lang="scss" scoped>
  161. @import "../../libs/css/components.scss";
  162. .u-swiper {
  163. @include flex;
  164. justify-content: center;
  165. align-items: center;
  166. position: relative;
  167. overflow: hidden;
  168. &__wrapper {
  169. flex: 1;
  170. &__item {
  171. flex: 1;
  172. &__wrapper {
  173. @include flex;
  174. position: relative;
  175. overflow: hidden;
  176. transition: transform 0.3s;
  177. flex: 1;
  178. &__image {
  179. flex: 1;
  180. }
  181. &__video {
  182. flex: 1;
  183. }
  184. &__title {
  185. position: absolute;
  186. background-color: rgba(0, 0, 0, 0.3);
  187. bottom: 0;
  188. left: 0;
  189. right: 0;
  190. font-size: 28rpx;
  191. padding: 12rpx 24rpx;
  192. color: #FFFFFF;
  193. flex: 1;
  194. }
  195. }
  196. }
  197. }
  198. &__indicator {
  199. position: absolute;
  200. bottom: 10px;
  201. }
  202. }
  203. </style>