u-swiper.vue 7.6 KB

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