uv-swiper.vue 8.7 KB

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