特易招,招聘小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
8.7 KiB

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