u-swiper.vue 8.8 KB

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