uv-swiper.vue 11 KB

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