swiper.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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="$u.test.image(getSource(item.image))"
  20. :src="getSource(item.image)" :mode="imgMode" @tap="clickHandler(index)" :style="{
  21. height: $u.addUnit(height/2),width: $u.addUnit(height/2),
  22. borderRadius: '50%'
  23. }"></image>
  24. <video class="u-swiper__wrapper__item__wrapper__video" v-if="$u.test.video(getSource(item))"
  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 class="">
  33. <text
  34. style="align-items: center;display: flex;justify-content: center;padding-top: 40rpx;">{{ item.title }}</text>
  35. </view>
  36. </view>
  37. </swiper-item>
  38. </swiper>
  39. <view class="u-swiper__indicator" :style="[$u.addStyle(indicatorStyle)]">
  40. <slot name="indicator">
  41. <u-swiper-indicator v-if="!loading && indicator && !showTitle"
  42. :indicatorActiveColor="indicatorActiveColor" :indicatorInactiveColor="indicatorInactiveColor"
  43. :length="list.length" :current="currentIndex" :indicatorMode="indicatorMode"></u-swiper-indicator>
  44. </slot>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. import props from './props.js';
  50. /**
  51. * Swiper 轮播图
  52. * @description 该组件一般用于导航轮播,广告展示等场景,可开箱即用,
  53. * @tutorial https://www.uviewui.com/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 <u-swiper :list="list4" keyName="url" :autoplay="false"></u-swiper>
  81. */
  82. export default {
  83. name: 'u-swiper',
  84. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  85. data() {
  86. return {
  87. currentIndex: 0
  88. }
  89. },
  90. watch: {
  91. current(val, preVal) {
  92. if (val === preVal) return;
  93. this.currentIndex = val; // 和上游数据关联上
  94. }
  95. },
  96. computed: {
  97. itemStyle() {
  98. return index => {
  99. const style = {}
  100. // #ifndef APP-NVUE || MP-TOUTIAO
  101. // 左右流出空间的写法不支持nvue和头条
  102. // 只有配置了此二值,才加上对应的圆角,以及缩放
  103. if (this.nextMargin && this.previousMargin) {
  104. style.borderRadius = uni.$u.addUnit(this.radius)
  105. if (index !== this.currentIndex) style.transform = 'scale(0.72)'
  106. if (index === this.currentIndex) style.border = '2px solid rgb(25, 146, 148)'
  107. }
  108. // #endif
  109. return style
  110. }
  111. }
  112. },
  113. methods: {
  114. // 获取目标路径,可能数组中为字符串,对象的形式,额外可指定对象的目标属性名keyName
  115. getSource(item) {
  116. if (typeof item === 'string') return item
  117. if (typeof item === 'object' && this.keyName) return item[this.keyName]
  118. else uni.$u.error('请按格式传递列表参数')
  119. return ''
  120. },
  121. // 轮播切换事件
  122. change(e) {
  123. // 当前的激活索引
  124. const {
  125. current
  126. } = e.detail
  127. this.pauseVideo(this.currentIndex)
  128. this.currentIndex = current
  129. this.$emit('change', e.detail)
  130. },
  131. // 切换轮播时,暂停视频播放
  132. pauseVideo(index) {
  133. const lastItem = this.getSource(this.list[index])
  134. if (uni.$u.test.video(lastItem)) {
  135. // 当视频隐藏时,暂停播放
  136. const video = uni.createVideoContext(`video-${index}`, this)
  137. video.pause()
  138. }
  139. },
  140. // 当一个轮播item为视频时,获取它的视频海报
  141. getPoster(item) {
  142. return typeof item === 'object' && item.poster ? item.poster : ''
  143. },
  144. // 点击某个item
  145. clickHandler(index) {
  146. this.$emit('click', index)
  147. }
  148. },
  149. }
  150. </script>
  151. <style lang="scss" scoped>
  152. @import "@/uni_modules/uview-ui/libs/css/components.scss";
  153. .u-swiper {
  154. @include flex;
  155. justify-content: center;
  156. align-items: center;
  157. position: relative;
  158. overflow: hidden;
  159. &__wrapper {
  160. flex: 1;
  161. &__item {
  162. flex: 1;
  163. &__wrapper {
  164. @include flex;
  165. position: relative;
  166. overflow: hidden;
  167. transition: transform 0.3s;
  168. flex: 1;
  169. flex-direction: column;
  170. align-items: center;
  171. justify-content: center;
  172. &__image {
  173. // flex: 1;
  174. }
  175. &__video {
  176. flex: 1;
  177. }
  178. &__title {
  179. position: absolute;
  180. background-color: rgba(0, 0, 0, 0.3);
  181. bottom: 0;
  182. left: 0;
  183. right: 0;
  184. font-size: 28rpx;
  185. padding: 12rpx 24rpx;
  186. color: #FFFFFF;
  187. flex: 1;
  188. }
  189. }
  190. }
  191. }
  192. &__indicator {
  193. position: absolute;
  194. bottom: 10px;
  195. }
  196. }
  197. </style>