su-swiper.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. <template>
  2. <view>
  3. <view class="ui-swiper" :class="[props.mode, props.bg, props.ui]">
  4. <swiper
  5. :circular="props.circular"
  6. :current="state.cur"
  7. :autoplay="props.autoplay && !state.videoPlaySataus"
  8. :interval="props.interval"
  9. :duration="props.duration"
  10. @transition="transition"
  11. @animationfinish="animationfinish"
  12. :style="customStyle"
  13. @change="swiperChange"
  14. >
  15. <swiper-item
  16. class="swiper-item"
  17. v-for="(item, index) in props.list"
  18. :key="index"
  19. :class="{ cur: state.cur == index }"
  20. @tap="onSwiperItem(item)"
  21. >
  22. <view class="ui-swiper-main">
  23. <image
  24. v-if="item.type === 'image'"
  25. class="swiper-image"
  26. :mode="props.imageMode"
  27. :src="item.src"
  28. width="100%"
  29. height="100%"
  30. @load="onImgLoad"
  31. ></image>
  32. <su-video
  33. v-else
  34. :ref="(el) => (refs.videoRef[`video_${index}`] = el)"
  35. :poster="sheep.$url.cdn(item.poster)"
  36. :src="sheep.$url.cdn(item.src)"
  37. :index="index"
  38. :moveX="state.moveX"
  39. :initialTime="item.currentTime || 0"
  40. :height="seizeHeight"
  41. @videoTimeupdate="videoTimeupdate"
  42. ></su-video>
  43. </view>
  44. </swiper-item>
  45. </swiper>
  46. <template v-if="!state.videoPlaySataus">
  47. <view class="ui-swiper-dot" :class="props.dotStyle" v-if="props.dotStyle != 'tag'">
  48. <view
  49. class="line-box"
  50. v-for="(item, index) in props.list"
  51. :key="index"
  52. :class="[state.cur == index ? 'cur' : '', props.dotCur]"
  53. ></view>
  54. </view>
  55. <view class="ui-swiper-dot" :class="props.dotStyle" v-if="props.dotStyle == 'tag'">
  56. <view
  57. class="ui-tag radius-lg"
  58. :class="[props.dotCur]"
  59. style="pointer-events: none; padding: 0 10rpx"
  60. >
  61. <view style="transform: scale(0.7)">{{ state.cur + 1 }} / {{ props.list.length }}</view>
  62. </view>
  63. </view>
  64. </template>
  65. </view>
  66. </view>
  67. </template>
  68. <script setup>
  69. /**
  70. * 轮播组件
  71. *
  72. * @property {Boolean} circular = false - 是否采用衔接滑动,即播放到末尾后重新回到开头
  73. * @property {Boolean} autoplay = true - 是否自动切换
  74. * @property {Number} interval = 5000 - 自动切换时间间隔
  75. * @property {Number} duration = 500 - 滑动动画时长,app-nvue不支持
  76. * @property {Array} list = [] - 轮播数据
  77. * @property {String} ui = '' - 样式class
  78. * @property {String} mode - 模式
  79. * @property {String} dotStyle - 指示点样式
  80. * @property {String} dotCur= 'ui-BG-Main' - 当前指示点样式,默认主题色
  81. * @property {String} bg - 背景
  82. * @property {String} height = 300 - 组件高度
  83. * @property {String} imgHeight = 300 - 图片高度
  84. *
  85. * @example list = [{url:'跳转路径',urlType:'跳转方式',type:'轮播类型',src:'轮播内容地址',poster:'视频必传'}]
  86. */
  87. import { reactive, computed } from 'vue';
  88. import sheep from '@/sheep';
  89. import { clone } from 'lodash';
  90. // 数据
  91. const state = reactive({
  92. imgHeight: 0,
  93. cur: 0,
  94. moveX: 0,
  95. videoPlaySataus: false,
  96. heightList: [],
  97. });
  98. const refs = reactive({
  99. videoRef: {},
  100. });
  101. // 接收参数
  102. const props = defineProps({
  103. circular: {
  104. type: Boolean,
  105. default: true,
  106. },
  107. autoplay: {
  108. type: Boolean,
  109. default: false,
  110. },
  111. interval: {
  112. type: Number,
  113. default: 3000,
  114. },
  115. duration: {
  116. type: Number,
  117. default: 500,
  118. },
  119. mode: {
  120. type: String,
  121. default: 'default',
  122. },
  123. imageMode: {
  124. type: String,
  125. default: 'scaleToFill',
  126. },
  127. list: {
  128. type: Array,
  129. default() {
  130. return [];
  131. },
  132. },
  133. dotStyle: {
  134. type: String,
  135. default: 'long', //default long tag
  136. },
  137. dotCur: {
  138. type: String,
  139. default: 'ss-bg-opactity-block',
  140. },
  141. bg: {
  142. type: String,
  143. default: 'bg-none',
  144. },
  145. height: {
  146. type: Number,
  147. default: 0,
  148. },
  149. imgHeight: {
  150. type: Number,
  151. default: 0,
  152. },
  153. imgTopRadius: {
  154. type: Number,
  155. default: 0,
  156. },
  157. imgBottomRadius: {
  158. type: Number,
  159. default: 0,
  160. },
  161. isPreview: {
  162. type: Boolean,
  163. default: false,
  164. },
  165. seizeHeight: {
  166. type: Number,
  167. default: 200,
  168. },
  169. });
  170. // current 改变时会触发 change 事件
  171. const swiperChange = (e) => {
  172. if (e.detail.source !== 'touch' && e.detail.source !== 'autoplay') return;
  173. state.cur = e.detail.current;
  174. state.videoPlaySataus = false;
  175. if (props.list[state.cur].type === 'video') {
  176. refs.videoRef[`video_${state.cur}`].pausePlay();
  177. }
  178. };
  179. // 点击轮播组件
  180. const onSwiperItem = (item) => {
  181. if (item.type === 'video') {
  182. state.videoPlaySataus = true;
  183. } else {
  184. if(item.urlParams && item.urlParams.title){
  185. sheep.$router.go(item.url,{title:item.urlParams.title});
  186. }else{
  187. sheep.$router.go(item.url);
  188. }
  189. onPreview();
  190. }
  191. };
  192. const onPreview = () => {
  193. if (!props.isPreview) return;
  194. let previewImage = clone(props.list);
  195. previewImage.forEach((item,index) => {
  196. if(item.type === 'video') {
  197. previewImage.splice(index, 1);
  198. }
  199. })
  200. uni.previewImage({
  201. urls:
  202. previewImage.length < 1
  203. ? [props.src]
  204. : previewImage.reduce((pre, cur) => {
  205. pre.push(cur.src);
  206. return pre;
  207. }, []),
  208. current: state.cur,
  209. // longPressActions: {
  210. // itemList: ['发送给朋友', '保存图片', '收藏'],
  211. // success: function (data) {
  212. // console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
  213. // },
  214. // fail: function (err) {
  215. // console.log(err.errMsg);
  216. // },
  217. // },
  218. });
  219. };
  220. //
  221. // swiper-item 的位置发生改变时会触发 transition
  222. const transition = (e) => {
  223. // #ifdef APP-PLUS
  224. state.moveX = e.detail.dx;
  225. // #endif
  226. };
  227. // 动画结束时会触发 animationfinish
  228. const animationfinish = (e) => {
  229. state.moveX = 0;
  230. };
  231. const videoTimeupdate = (e) => {
  232. props.list[state.cur].currentTime = e.detail.currentTime;
  233. };
  234. // 自动计算高度
  235. const customStyle = computed(() => {
  236. let height;
  237. // 固定高度情况
  238. if (props.height !== 0) {
  239. height = props.height;
  240. }
  241. // 自动高度情况
  242. if (props.height === 0) {
  243. // 图片预加载占位高度
  244. if (state.imgHeight !== 0) {
  245. height = state.imgHeight;
  246. } else if (props.seizeHeight !== 0) {
  247. height = props.seizeHeight;
  248. }
  249. }
  250. return {
  251. height: height + 'rpx',
  252. };
  253. });
  254. // 计算轮播图片最大高度
  255. function onImgLoad(e) {
  256. if (props.height === 0) {
  257. let newHeight = (e.detail.height / e.detail.width) * 750;
  258. if (state.imgHeight < newHeight) {
  259. state.imgHeight = newHeight;
  260. }
  261. }
  262. }
  263. </script>
  264. <style lang="scss" scoped>
  265. .ui-swiper {
  266. position: relative;
  267. .ui-swiper-main {
  268. width: 100%;
  269. height: 100%;
  270. }
  271. .ui-swiper-main .swiper-image {
  272. width: 100%;
  273. height: 100%;
  274. }
  275. .ui-swiper-dot {
  276. position: absolute;
  277. width: 100%;
  278. bottom: 20rpx;
  279. height: 30rpx;
  280. display: flex;
  281. align-items: center;
  282. justify-content: center;
  283. &.default .line-box {
  284. display: inline-flex;
  285. border-radius: 50rpx;
  286. width: 6px;
  287. height: 6px;
  288. border: 2px solid transparent;
  289. margin: 0 10rpx;
  290. opacity: 0.3;
  291. position: relative;
  292. justify-content: center;
  293. align-items: center;
  294. &.cur {
  295. width: 8px;
  296. height: 8px;
  297. opacity: 1;
  298. border: 0px solid transparent;
  299. }
  300. &.cur::after {
  301. content: '';
  302. border-radius: 50rpx;
  303. width: 4px;
  304. height: 4px;
  305. background-color: #fff;
  306. }
  307. }
  308. &.long .line-box {
  309. display: inline-block;
  310. border-radius: 100rpx;
  311. width: 6px;
  312. height: 6px;
  313. margin: 0 10rpx;
  314. opacity: 0.3;
  315. position: relative;
  316. &.cur {
  317. width: 24rpx;
  318. opacity: 1;
  319. }
  320. &.cur::after {
  321. }
  322. }
  323. &.line {
  324. bottom: 20rpx;
  325. .line-box {
  326. display: inline-block;
  327. width: 30px;
  328. height: 3px;
  329. opacity: 0.3;
  330. position: relative;
  331. &.cur {
  332. opacity: 1;
  333. }
  334. }
  335. }
  336. &.tag {
  337. justify-content: flex-end;
  338. position: absolute;
  339. bottom: 20rpx;
  340. right: 20rpx;
  341. }
  342. }
  343. &.card {
  344. .swiper-item {
  345. width: 610rpx !important;
  346. left: 70rpx;
  347. box-sizing: border-box;
  348. padding: 20rpx 0rpx 60rpx;
  349. overflow: initial;
  350. }
  351. .swiper-item .ui-swiper-main {
  352. width: 100%;
  353. display: block;
  354. height: 100%;
  355. transform: scale(0.9);
  356. transition: all 0.2s ease-in 0s;
  357. position: relative;
  358. background-size: cover;
  359. .swiper-image {
  360. height: 100%;
  361. }
  362. }
  363. .swiper-item .ui-swiper-main::before {
  364. content: '';
  365. display: block;
  366. background: inherit;
  367. filter: blur(5px);
  368. position: absolute;
  369. width: 100%;
  370. height: 100%;
  371. top: 10rpx;
  372. left: 10rpx;
  373. z-index: -1;
  374. opacity: 0.3;
  375. transform-origin: 0 0;
  376. transform: scale(1, 1);
  377. }
  378. .swiper-item.cur .ui-swiper-main {
  379. transform: scale(1);
  380. transition: all 0.2s ease-in 0s;
  381. }
  382. .ui-swiper-dot.tag {
  383. position: absolute;
  384. bottom: 85rpx;
  385. right: 75rpx;
  386. }
  387. }
  388. &.hotelCard {
  389. .swiper-item {
  390. width: 650rpx !important;
  391. left: 30rpx;
  392. box-sizing: border-box;
  393. padding: 0rpx 0rpx 50rpx;
  394. overflow: initial;
  395. }
  396. .swiper-item .ui-swiper-main {
  397. width: 100%;
  398. display: block;
  399. height: 100%;
  400. transform: scale(0.9);
  401. opacity: 0.8;
  402. transition: all 0.2s ease-in 0s;
  403. position: relative;
  404. background-size: cover;
  405. .swiper-image {
  406. width: 100%;
  407. height: 400rpx;
  408. }
  409. }
  410. .swiper-item .ui-swiper-main::before {
  411. content: '';
  412. display: block;
  413. background: inherit;
  414. filter: blur(5px);
  415. position: absolute;
  416. width: 100%;
  417. height: 100%;
  418. top: 10rpx;
  419. left: 10rpx;
  420. z-index: -1;
  421. opacity: 0.3;
  422. transform-origin: 0 0;
  423. transform: scale(1, 1);
  424. }
  425. .swiper-item.cur .ui-swiper-main {
  426. transform: scale(1);
  427. transition: all 0.2s ease-in 0s;
  428. opacity: 1;
  429. }
  430. .ui-swiper-dot {
  431. display: none;
  432. }
  433. }
  434. &.hotelDetail {
  435. .swiper-item {
  436. width: 690rpx !important;
  437. left: 30rpx;
  438. box-sizing: border-box;
  439. padding: 20rpx 0rpx;
  440. overflow: initial;
  441. }
  442. .swiper-item .ui-swiper-main {
  443. width: 100%;
  444. display: block;
  445. height: 100%;
  446. transform: scale(0.96);
  447. transition: all 0.2s ease-in 0s;
  448. position: relative;
  449. background-size: cover;
  450. .swiper-image {
  451. height: 100%;
  452. }
  453. }
  454. .swiper-item.cur .ui-swiper-main {
  455. transform: scale(0.96);
  456. transition: all 0.2s ease-in 0s;
  457. }
  458. }
  459. }
  460. </style>