u-swiper.vue 8.5 KB

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