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.

267 lines
8.6 KiB

5 months ago
  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. <u-loading-icon mode="circle"></u-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. <view
  42. class="u-swiper__wrapper__item__wrapper"
  43. :style="[itemStyle(index)]"
  44. >
  45. <!-- 在nvue中image图片的宽度默认为屏幕宽度需要通过flex:1撑开另外必须设置高度才能显示图片 -->
  46. <image
  47. class="u-swiper__wrapper__item__wrapper__image"
  48. v-if="getItemType(item) === 'image'"
  49. :src="getSource(item)"
  50. :mode="imgMode"
  51. @tap="clickHandler(index)"
  52. :style="{
  53. height: addUnit(height),
  54. borderRadius: addUnit(radius)
  55. }"
  56. ></image>
  57. <video
  58. class="u-swiper__wrapper__item__wrapper__video"
  59. v-if="getItemType(item) === 'video'"
  60. :id="`video-${index}`"
  61. :enable-progress-gesture="false"
  62. :src="getSource(item)"
  63. :poster="getPoster(item)"
  64. :title="showTitle && testObject(item) && item.title ? item.title : ''"
  65. :style="{
  66. height: addUnit(height)
  67. }"
  68. controls
  69. @tap="clickHandler(index)"
  70. ></video>
  71. <text
  72. v-if="showTitle && testObject(item) && item.title && testImage(getSource(item))"
  73. class="u-swiper__wrapper__item__wrapper__title u-line-1"
  74. >{{ item.title }}</text>
  75. </view>
  76. </swiper-item>
  77. </swiper>
  78. <view class="u-swiper__indicator" :style="[addStyle(indicatorStyle)]">
  79. <slot name="indicator">
  80. <u-swiper-indicator
  81. v-if="!loading && indicator && !showTitle"
  82. :indicatorActiveColor="indicatorActiveColor"
  83. :indicatorInactiveColor="indicatorInactiveColor"
  84. :length="list.length"
  85. :current="currentIndex"
  86. :indicatorMode="indicatorMode"
  87. ></u-swiper-indicator>
  88. </slot>
  89. </view>
  90. </view>
  91. </template>
  92. <script>
  93. import { props } from './props.js';
  94. import { mpMixin } from '../../libs/mixin/mpMixin';
  95. import { mixin } from '../../libs/mixin/mixin';
  96. import { addUnit, addStyle, error } from '../../libs/function/index';
  97. import test from '../../libs/function/test';
  98. /**
  99. * Swiper 轮播图
  100. * @description 该组件一般用于导航轮播广告展示等场景,可开箱即用
  101. * @tutorial https://ijry.github.io/uview-plus/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 <u-swiper :list="list4" keyName="url" :autoplay="false"></u-swiper>
  129. */
  130. export default {
  131. name: 'u-swiper',
  132. mixins: [mpMixin, mixin, props],
  133. data() {
  134. return {
  135. currentIndex: 0
  136. }
  137. },
  138. watch: {
  139. current(val, preVal) {
  140. if(val === preVal) return;
  141. this.currentIndex = val; // 和上游数据关联上
  142. }
  143. },
  144. emits: ["click", "change"],
  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 = 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. addStyle,
  163. addUnit,
  164. testObject: test.object,
  165. testImage: test.image,
  166. getItemType(item) {
  167. if (typeof item === 'string') return test.video(this.getSource(item)) ? 'video' : 'image'
  168. if (typeof item === 'object' && this.keyName) {
  169. if (!item.type) return test.video(this.getSource(item)) ? 'video' : 'image'
  170. if (item.type === 'image') return 'image'
  171. if (item.type === 'video') return 'video'
  172. return 'image'
  173. }
  174. },
  175. // 获取目标路径,可能数组中为字符串,对象的形式,额外可指定对象的目标属性名keyName
  176. getSource(item) {
  177. if (typeof item === 'string') return item
  178. if (typeof item === 'object' && this.keyName) return item[this.keyName]
  179. else error('请按格式传递列表参数')
  180. return ''
  181. },
  182. // 轮播切换事件
  183. change(e) {
  184. // 当前的激活索引
  185. const {
  186. current
  187. } = e.detail
  188. this.pauseVideo(this.currentIndex)
  189. this.currentIndex = current
  190. this.$emit('change', e.detail)
  191. },
  192. // 切换轮播时,暂停视频播放
  193. pauseVideo(index) {
  194. const lastItem = this.getSource(this.list[index])
  195. if (test.video(lastItem)) {
  196. // 当视频隐藏时,暂停播放
  197. const video = uni.createVideoContext(`video-${index}`, this)
  198. video.pause()
  199. }
  200. },
  201. // 当一个轮播item为视频时,获取它的视频海报
  202. getPoster(item) {
  203. return typeof item === 'object' && item.poster ? item.poster : ''
  204. },
  205. // 点击某个item
  206. clickHandler(index) {
  207. this.$emit('click', index)
  208. }
  209. },
  210. }
  211. </script>
  212. <style lang="scss" scoped>
  213. @import "../../libs/css/components.scss";
  214. .u-swiper__wrapper {
  215. flex: 1;
  216. }
  217. .u-swiper {
  218. @include flex;
  219. justify-content: center;
  220. align-items: center;
  221. position: relative;
  222. overflow: hidden;
  223. &__wrapper {
  224. flex: 1;
  225. &__item {
  226. flex: 1;
  227. &__wrapper {
  228. @include flex;
  229. position: relative;
  230. overflow: hidden;
  231. transition: transform 0.3s;
  232. flex: 1;
  233. &__image {
  234. flex: 1;
  235. }
  236. &__video {
  237. flex: 1;
  238. }
  239. &__title {
  240. position: absolute;
  241. background-color: rgba(0, 0, 0, 0.3);
  242. bottom: 0;
  243. left: 0;
  244. right: 0;
  245. font-size: 28rpx;
  246. padding: 12rpx 24rpx;
  247. color: #FFFFFF;
  248. flex: 1;
  249. }
  250. }
  251. }
  252. }
  253. &__indicator {
  254. position: absolute;
  255. bottom: 10px;
  256. }
  257. }
  258. </style>