1
0

u-swiper-plus.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 mode="circle"></u-loading>
  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="autoplayCom"
  27. :current="currentIndex"
  28. :currentItemId="currentItemId"
  29. :previousMargin="$u.addUnit(previousMargin)"
  30. :nextMargin="$u.addUnit(nextMargin)"
  31. :acceleration="acceleration"
  32. :displayMultipleItems="displayMultipleItemsCom"
  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. <!-- #ifdef H5 || MP-WEIXIN || APP-NVUE -->
  57. <!-- 只有h5和微信小程序和nvue可以直接播放视频 -->
  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,'auto')"
  65. :title="showTitle && $u.test.object(item) && item.title ? item.title : ''"
  66. :style="{
  67. height: $u.addUnit(height)
  68. }"
  69. :controls="true"
  70. :show-mute-btn="true"
  71. :autoplay="videoAutoplayFn(index)"
  72. :muted="videoMuted"
  73. :object-fit="item.objectFit"
  74. @tap="clickHandler(index)"
  75. @ended="videoEnded(index)"
  76. ></video>
  77. <!-- #endif -->
  78. <!-- #ifndef H5 || MP-WEIXIN || APP-NVUE-->
  79. <!-- 其他端只能展示图片,点击图片播放视频 -->
  80. <image
  81. v-if="getItemType(item) === 'video'"
  82. class="u-swiper__wrapper__item__wrapper__image"
  83. :src="getPoster(item)"
  84. mode="aspectFill"
  85. :style="{
  86. height: $u.addUnit(height),
  87. borderRadius: $u.addUnit(radius)
  88. }"
  89. ></image>
  90. <image
  91. v-if="getItemType(item) === 'video'"
  92. class="play-btn"
  93. src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKAAAACgCAYAAACLz2ctAAAAAXNSR0IArs4c6QAAEQZJREFUeF7tXQ1sVtUZfkTQgSYCtehE18gcm9L5O1CnYlCGjX8bMpGNZJtrBhbKm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r0tWIu+p0X7FAsyu88z8cjXdTvSxbTCWiDwHVDfh9y5szlm6i0q2cnc5jlcgol8vzO83Qo3BcGeSwkih3FCUuuNTzzmzEKxm86DrN8ut23a3rjokjArn3CRe2LAVxkbfOZcu8ddym4XbYLwIe6LR77SfqoE7ArVlxTHAbgQmuYztZoqObQesAaXj8B8B8A2q7dCQH9QYCEZDgRzqh5G9QQAFnWnrQ/NSQvhW83DqOfW7cIcebKMBexIFxPSOL0BlQhFSczDLxJwaz9nGnNsLklyIcBOTmUEzuSmEby8C1GclHmxO0vPpyptlpCT4o9+VCBYvzkQQVMlTT/BwkcOjQQnflAAAAAAElFTkSuQmCC"
  94. mode="widthFix"
  95. @tap.stop="previewVideo(index)"
  96. ></image>
  97. <!-- #endif -->
  98. <text
  99. v-if="showTitle && $u.test.object(item) && item.title && $u.test.image(getSource(item))"
  100. class="u-swiper__wrapper__item__wrapper__title u-line-1"
  101. >{{ item.title }}</text>
  102. </view>
  103. </swiper-item>
  104. </swiper>
  105. <view class="u-swiper__indicator" :style="[$u.addStyle(indicatorStyle)]">
  106. <slot name="indicator">
  107. <u-swiper-indicator
  108. v-if="!loading && indicator && !showTitle"
  109. :indicatorActiveColor="indicatorActiveColor"
  110. :indicatorInactiveColor="indicatorInactiveColor"
  111. :length="list.length"
  112. :current="currentIndex"
  113. :indicatorMode="indicatorMode"
  114. ></u-swiper-indicator>
  115. </slot>
  116. </view>
  117. <u-popup v-model="videoPopup.show" mode="center" :duration="0" :blur="10" :closeable="true">
  118. <video
  119. v-if="videoPopup.show"
  120. class="u-swiper__wrapper__item__wrapper__video"
  121. :enable-progress-gesture="false"
  122. :src="videoPopup.url"
  123. :poster="videoPopup.poster"
  124. style="height: 60vh;"
  125. :controls="true"
  126. :show-mute-btn="true"
  127. :autoplay="true"
  128. :muted="false"
  129. object-fit="contain"
  130. @ended="videoEnded(index)"
  131. ></video>
  132. </u-popup>
  133. </view>
  134. </template>
  135. <script>
  136. import uSwiperIndicator from "./components/u-swiper-indicator"
  137. /**
  138. * Swiper 轮播图
  139. * @description 该组件一般用于导航轮播,广告展示等场景,可开箱即用,
  140. * @tutorial https://www.uviewui.com/components/swiper.html
  141. * @property {Array} list 轮播图数据
  142. * @property {Boolean} indicator 是否显示面板指示器(默认 false )
  143. * @property {String} indicatorActiveColor 指示器非激活颜色(默认 '#FFFFFF' )
  144. * @property {String} indicatorInactiveColor 指示器的激活颜色(默认 'rgba(255, 255, 255, 0.35)' )
  145. * @property {String | Object} indicatorStyle 指示器样式,可通过bottom,left,right进行定位
  146. * @property {String} indicatorMode 指示器模式(默认 'line' )
  147. * @property {Boolean} autoplay 是否自动切换(默认 true )
  148. * @property {String | Number} current 当前所在滑块的 index(默认 0 )
  149. * @property {String} currentItemId 当前所在滑块的 item-id ,不能与 current 被同时指定
  150. * @property {String | Number} interval 滑块自动切换时间间隔(ms)(默认 3000 )
  151. * @property {String | Number} duration 滑块切换过程所需时间(ms)(默认 300 )
  152. * @property {Boolean} circular 播放到末尾后是否重新回到开头(默认 false )
  153. * @property {String | Number} previousMargin 前边距,可用于露出前一项的一小部分,nvue和支付宝不支持(默认 0 )
  154. * @property {String | Number} nextMargin 后边距,可用于露出后一项的一小部分,nvue和支付宝不支持(默认 0 )
  155. * @property {Boolean} acceleration 当开启时,会根据滑动速度,连续滑动多屏,支付宝不支持(默认 false )
  156. * @property {Number} displayMultipleItems 同时显示的滑块数量,nvue、支付宝小程序不支持(默认 1 )
  157. * @property {String} easingFunction 指定swiper切换缓动动画类型, 只对微信小程序有效(默认 'default' )
  158. * @property {String} keyName list数组中指定对象的目标属性名(默认 'url' )
  159. * @property {String} imgMode 图片的裁剪模式(默认 'aspectFill' )
  160. * @property {String | Number} height 组件高度(默认 130 )
  161. * @property {String} bgColor 背景颜色(默认 '#f3f4f6' )
  162. * @property {String | Number} radius 组件圆角,数值或带单位的字符串(默认 4 )
  163. * @property {Boolean} loading 是否加载中(默认 false )
  164. * @property {Boolean} showTitle 是否显示标题,要求数组对象中有title属性(默认 false )
  165. * @event {Function(index)} click 点击轮播图时触发 index:点击了第几张图片,从0开始
  166. * @event {Function(index)} change 轮播图切换时触发(自动或者手动切换) index:切换到了第几张图片,从0开始
  167. * @example <u-swiper :list="list4" keyName="url" :autoplay="false"></u-swiper>
  168. */
  169. export default {
  170. name: 'u-swiper',
  171. emits: ["click", "change"],
  172. components:{
  173. uSwiperIndicator
  174. },
  175. props: {
  176. // 列表数组,元素可为字符串,如为对象可通过keyName指定目标属性名
  177. list: {
  178. type: [Array, null],
  179. default () {
  180. return [];
  181. }
  182. },
  183. // 是否显示面板指示器
  184. indicator: {
  185. type: Boolean,
  186. default: false
  187. },
  188. // 指示器非激活颜色
  189. indicatorActiveColor: {
  190. type: String,
  191. default: "#FFFFFF"
  192. },
  193. // 指示器的激活颜色
  194. indicatorInactiveColor: {
  195. type: String,
  196. default: "rgba(255, 255, 255, 0.35)"
  197. },
  198. // 指示器样式,可通过bottom,left,right进行定位
  199. indicatorStyle: {
  200. type: [String, Object],
  201. default: '',
  202. },
  203. // 指示器模式,line-线型,dot-点型
  204. indicatorMode: {
  205. type: String,
  206. default: 'line',
  207. },
  208. // 是否自动切换
  209. autoplay: {
  210. type: Boolean,
  211. default: true
  212. },
  213. // 当前所在滑块的 index
  214. current: {
  215. type: [String, Number],
  216. default: 0
  217. },
  218. // 当前所在滑块的 item-id ,不能与 current 被同时指定
  219. currentItemId: {
  220. type: String,
  221. default: '',
  222. },
  223. // 滑块自动切换时间间隔
  224. interval: {
  225. type: [String, Number],
  226. default: 3000
  227. },
  228. // 滑块切换过程所需时间
  229. duration: {
  230. type: [String, Number],
  231. default: 300
  232. },
  233. // 播放到末尾后是否重新回到开头
  234. circular: {
  235. type: Boolean,
  236. default: false
  237. },
  238. // 前边距,可用于露出前一项的一小部分,nvue和支付宝不支持
  239. previousMargin: {
  240. type: [String, Number],
  241. default: 0
  242. },
  243. // 后边距,可用于露出后一项的一小部分,nvue和支付宝不支持
  244. nextMargin: {
  245. type: [String, Number],
  246. default: 0
  247. },
  248. // 当开启时,会根据滑动速度,连续滑动多屏,支付宝不支持
  249. acceleration: {
  250. type: Boolean,
  251. default: false
  252. },
  253. // 同时显示的滑块数量,nvue、支付宝小程序不支持
  254. displayMultipleItems: {
  255. type: Number,
  256. default: 1
  257. },
  258. // 指定swiper切换缓动动画类型,有效值:default、linear、easeInCubic、easeOutCubic、easeInOutCubic
  259. // 只对微信小程序有效
  260. easingFunction: {
  261. type: String,
  262. default: 'default'
  263. },
  264. // list数组中指定对象的目标属性名
  265. keyName: {
  266. type: String,
  267. default: 'url'
  268. },
  269. // 图片的裁剪模式
  270. imgMode: {
  271. type: String,
  272. default: 'aspectFill'
  273. },
  274. // 组件高度
  275. height: {
  276. type: [String, Number],
  277. default: 130
  278. },
  279. // 背景颜色
  280. bgColor: {
  281. type: String,
  282. default: '#f3f4f6'
  283. },
  284. // 组件圆角,数值或带单位的字符串
  285. radius: {
  286. type: [String, Number],
  287. default: 4
  288. },
  289. // 是否加载中
  290. loading: {
  291. type: Boolean,
  292. default: false
  293. },
  294. // 是否显示标题,要求数组对象中有title属性
  295. showTitle: {
  296. type: Boolean,
  297. default: false
  298. },
  299. // 视频是否自动播放
  300. videoAutoplay: {
  301. type: Boolean,
  302. default: false
  303. },
  304. // 视频是否静音播放
  305. videoMuted: {
  306. type: Boolean,
  307. default: false
  308. },
  309. },
  310. data() {
  311. return {
  312. $u: uni.$u,
  313. currentIndex: 0,
  314. videoPopup: {
  315. show: false,
  316. url: ""
  317. }
  318. }
  319. },
  320. watch: {
  321. current(val, preVal) {
  322. if(val === preVal) return;
  323. this.currentIndex = val; // 和上游数据关联上
  324. },
  325. // #ifdef H5 || MP-WEIXIN || APP-NVUE
  326. currentIndex(newVal, oldVal){
  327. if (this.checkIsVideo(oldVal)) {
  328. // 如果是视频,则暂停
  329. this.videoPopup.show = false;
  330. this.videoPopup.url = "";
  331. let videoContext = uni.createVideoContext(`video-${oldVal}`, this);
  332. videoContext.pause();
  333. videoContext.exitFullScreen();
  334. }
  335. if (this.checkIsVideo(newVal) && this.videoAutoplay) {
  336. // 如果是视频,则播放
  337. let videoContext = uni.createVideoContext(`video-${newVal}`, this);
  338. videoContext.play();
  339. }
  340. }
  341. // #endif
  342. },
  343. computed: {
  344. itemStyle() {
  345. return index => {
  346. const style = {}
  347. // #ifndef APP-NVUE || MP-TOUTIAO
  348. // 左右流出空间的写法不支持nvue和头条
  349. // 只有配置了此二值,才加上对应的圆角,以及缩放
  350. if (this.nextMargin && this.previousMargin) {
  351. style.borderRadius = uni.$u.addUnit(this.radius)
  352. if (index !== this.currentIndex) style.transform = 'scale(0.92)'
  353. }
  354. // #endif
  355. return style
  356. }
  357. },
  358. mutedCom() {
  359. // #ifdef H5
  360. return true;
  361. // #endif
  362. // #ifndef H5
  363. return false;
  364. // #endif
  365. },
  366. currentIsVideoCom(){
  367. let { currentIndex } = this;
  368. return this.checkIsVideo(currentIndex);
  369. },
  370. autoplayCom() {
  371. let { autoplay, currentIsVideoCom } = this;
  372. return currentIsVideoCom ? false : autoplay;
  373. },
  374. displayMultipleItemsCom(){
  375. let { displayMultipleItems, list } = this;
  376. if (list && list.length > 0) {
  377. return displayMultipleItems;
  378. } else {
  379. return 0
  380. }
  381. }
  382. },
  383. methods: {
  384. getItemType(item) {
  385. if (typeof item === 'string') return uni.$u.test.video(this.getSource(item)) ? 'video' : 'image'
  386. if (typeof item === 'object' && this.keyName) {
  387. if (!item.type) return uni.$u.test.video(this.getSource(item)) ? 'video' : 'image'
  388. if (item.type === 'image') return 'image'
  389. if (item.type === 'video') return 'video'
  390. return 'image'
  391. }
  392. },
  393. // 获取目标路径,可能数组中为字符串,对象的形式,额外可指定对象的目标属性名keyName
  394. getSource(item) {
  395. if (typeof item === 'string') return item
  396. if (typeof item === 'object' && this.keyName) return item[this.keyName]
  397. else uni.$u.error('请按格式传递列表参数')
  398. return ''
  399. },
  400. // 轮播切换事件
  401. change(e) {
  402. // 当前的激活索引
  403. const {
  404. current
  405. } = e.detail
  406. this.currentIndex = current
  407. this.$emit('change', e.detail)
  408. },
  409. // 当一个轮播item为视频时,获取它的视频海报
  410. getPoster(item, mode) {
  411. let { height } = this;
  412. if (mode === "auto") {
  413. return;
  414. }
  415. if (typeof item === 'object' && item.poster) {
  416. return item.poster;
  417. }
  418. let url = this.getSource(item);
  419. if (url.indexOf("cdn.bspapp.com")>-1 || url.indexOf("aliyuncs.com")>-1) {
  420. // 只有阿里云oss才支持
  421. let poster = `${url}?x-oss-process=video/snapshot,t_2000,f_jpg,w_0,h_0,m_fast,ar_auto`;
  422. return poster;
  423. } else {
  424. return;
  425. }
  426. },
  427. // 点击某个item
  428. clickHandler(index) {
  429. this.$emit('click', index)
  430. },
  431. // 下一页
  432. nextPage(){
  433. let { currentIndex=0, list=[] } = this;
  434. currentIndex++;
  435. currentIndex = currentIndex % list.length;
  436. this.currentIndex = currentIndex;
  437. },
  438. // 视频播放完毕后
  439. videoEnded(index){
  440. if (!this.videoPopup.show) {
  441. setTimeout(() => {
  442. this.nextPage();
  443. }, 1000);
  444. }
  445. },
  446. // 检测当前轮播显示的是否是视频
  447. checkIsVideo(index){
  448. let { list, currentIndex } = this;
  449. if (typeof index === "undefined") index = currentIndex;
  450. let item = list[index];
  451. return this.getItemType(item) === "video" ? true : false;
  452. },
  453. // 预览视频
  454. previewVideo(index){
  455. let { list, currentIndex } = this;
  456. if (typeof index === "undefined") index = currentIndex;
  457. let item = list[index];
  458. this.videoPopup.show = true;
  459. this.videoPopup.url = this.getSource(item);
  460. },
  461. videoAutoplayFn(index){
  462. let { list, current, videoAutoplay } = this;
  463. if (index === current && videoAutoplay) {
  464. return true;
  465. } else {
  466. return false;
  467. }
  468. }
  469. }
  470. }
  471. </script>
  472. <style lang="scss" scoped>
  473. @import "../../libs/css/style.components.scss";
  474. .u-swiper {
  475. @include vue-flex;
  476. justify-content: center;
  477. align-items: center;
  478. position: relative;
  479. overflow: hidden;
  480. &__wrapper {
  481. flex: 1;
  482. &__item {
  483. flex: 1;
  484. &__wrapper {
  485. @include vue-flex;
  486. position: relative;
  487. overflow: hidden;
  488. transition: transform 0.3s;
  489. flex: 1;
  490. &__image {
  491. flex: 1;
  492. }
  493. &__video {
  494. flex: 1;
  495. }
  496. &__title {
  497. position: absolute;
  498. background-color: rgba(0, 0, 0, 0.3);
  499. bottom: 0;
  500. left: 0;
  501. right: 0;
  502. font-size: 28rpx;
  503. padding: 12rpx 24rpx;
  504. color: #FFFFFF;
  505. flex: 1;
  506. }
  507. }
  508. }
  509. }
  510. &__indicator {
  511. position: absolute;
  512. bottom: 10px;
  513. }
  514. .play-btn{
  515. position: absolute;
  516. width: 100rpx;
  517. height: 100rpx;
  518. top:calc(50% - 50rpx);
  519. left:calc(50% - 50rpx);
  520. display: block;
  521. z-index: 99;
  522. }
  523. }
  524. </style>