su-swiper.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. sheep.$router.go(item.url);
  185. onPreview();
  186. }
  187. };
  188. const onPreview = () => {
  189. if (!props.isPreview) return;
  190. let previewImage = clone(props.list);
  191. previewImage.forEach((item,index) => {
  192. if(item.type === 'video') {
  193. previewImage.splice(index, 1);
  194. }
  195. })
  196. uni.previewImage({
  197. urls:
  198. previewImage.length < 1
  199. ? [props.src]
  200. : previewImage.reduce((pre, cur) => {
  201. pre.push(cur.src);
  202. return pre;
  203. }, []),
  204. current: state.cur,
  205. // longPressActions: {
  206. // itemList: ['发送给朋友', '保存图片', '收藏'],
  207. // success: function (data) {
  208. // console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
  209. // },
  210. // fail: function (err) {
  211. // console.log(err.errMsg);
  212. // },
  213. // },
  214. });
  215. };
  216. //
  217. // swiper-item 的位置发生改变时会触发 transition
  218. const transition = (e) => {
  219. // #ifdef APP-PLUS
  220. state.moveX = e.detail.dx;
  221. // #endif
  222. };
  223. // 动画结束时会触发 animationfinish
  224. const animationfinish = (e) => {
  225. state.moveX = 0;
  226. };
  227. const videoTimeupdate = (e) => {
  228. props.list[state.cur].currentTime = e.detail.currentTime;
  229. };
  230. // 自动计算高度
  231. const customStyle = computed(() => {
  232. let height;
  233. // 固定高度情况
  234. if (props.height !== 0) {
  235. height = props.height;
  236. }
  237. // 自动高度情况
  238. if (props.height === 0) {
  239. // 图片预加载占位高度
  240. if (state.imgHeight !== 0) {
  241. height = state.imgHeight;
  242. } else if (props.seizeHeight !== 0) {
  243. height = props.seizeHeight;
  244. }
  245. }
  246. return {
  247. height: height + 'rpx',
  248. };
  249. });
  250. // 计算轮播图片最大高度
  251. function onImgLoad(e) {
  252. if (props.height === 0) {
  253. let newHeight = (e.detail.height / e.detail.width) * 750;
  254. if (state.imgHeight < newHeight) {
  255. state.imgHeight = newHeight;
  256. }
  257. }
  258. }
  259. </script>
  260. <style lang="scss" scoped>
  261. .ui-swiper {
  262. position: relative;
  263. .ui-swiper-main {
  264. width: 100%;
  265. height: 100%;
  266. }
  267. .ui-swiper-main .swiper-image {
  268. width: 100%;
  269. height: 100%;
  270. }
  271. .ui-swiper-dot {
  272. position: absolute;
  273. width: 100%;
  274. bottom: 20rpx;
  275. height: 30rpx;
  276. display: flex;
  277. align-items: center;
  278. justify-content: center;
  279. &.default .line-box {
  280. display: inline-flex;
  281. border-radius: 50rpx;
  282. width: 6px;
  283. height: 6px;
  284. border: 2px solid transparent;
  285. margin: 0 10rpx;
  286. opacity: 0.3;
  287. position: relative;
  288. justify-content: center;
  289. align-items: center;
  290. &.cur {
  291. width: 8px;
  292. height: 8px;
  293. opacity: 1;
  294. border: 0px solid transparent;
  295. }
  296. &.cur::after {
  297. content: '';
  298. border-radius: 50rpx;
  299. width: 4px;
  300. height: 4px;
  301. background-color: #fff;
  302. }
  303. }
  304. &.long .line-box {
  305. display: inline-block;
  306. border-radius: 100rpx;
  307. width: 6px;
  308. height: 6px;
  309. margin: 0 10rpx;
  310. opacity: 0.3;
  311. position: relative;
  312. &.cur {
  313. width: 24rpx;
  314. opacity: 1;
  315. }
  316. &.cur::after {
  317. }
  318. }
  319. &.line {
  320. bottom: 20rpx;
  321. .line-box {
  322. display: inline-block;
  323. width: 30px;
  324. height: 3px;
  325. opacity: 0.3;
  326. position: relative;
  327. &.cur {
  328. opacity: 1;
  329. }
  330. }
  331. }
  332. &.tag {
  333. justify-content: flex-end;
  334. position: absolute;
  335. bottom: 20rpx;
  336. right: 20rpx;
  337. }
  338. }
  339. &.card {
  340. .swiper-item {
  341. width: 610rpx !important;
  342. left: 70rpx;
  343. box-sizing: border-box;
  344. padding: 20rpx 0rpx 60rpx;
  345. overflow: initial;
  346. }
  347. .swiper-item .ui-swiper-main {
  348. width: 100%;
  349. display: block;
  350. height: 100%;
  351. transform: scale(0.9);
  352. transition: all 0.2s ease-in 0s;
  353. position: relative;
  354. background-size: cover;
  355. .swiper-image {
  356. height: 100%;
  357. }
  358. }
  359. .swiper-item .ui-swiper-main::before {
  360. content: '';
  361. display: block;
  362. background: inherit;
  363. filter: blur(5px);
  364. position: absolute;
  365. width: 100%;
  366. height: 100%;
  367. top: 10rpx;
  368. left: 10rpx;
  369. z-index: -1;
  370. opacity: 0.3;
  371. transform-origin: 0 0;
  372. transform: scale(1, 1);
  373. }
  374. .swiper-item.cur .ui-swiper-main {
  375. transform: scale(1);
  376. transition: all 0.2s ease-in 0s;
  377. }
  378. .ui-swiper-dot.tag {
  379. position: absolute;
  380. bottom: 85rpx;
  381. right: 75rpx;
  382. }
  383. }
  384. &.hotelCard {
  385. .swiper-item {
  386. width: 650rpx !important;
  387. left: 30rpx;
  388. box-sizing: border-box;
  389. padding: 0rpx 0rpx 50rpx;
  390. overflow: initial;
  391. }
  392. .swiper-item .ui-swiper-main {
  393. width: 100%;
  394. display: block;
  395. height: 100%;
  396. transform: scale(0.9);
  397. opacity: 0.8;
  398. transition: all 0.2s ease-in 0s;
  399. position: relative;
  400. background-size: cover;
  401. .swiper-image {
  402. width: 100%;
  403. height: 400rpx;
  404. }
  405. }
  406. .swiper-item .ui-swiper-main::before {
  407. content: '';
  408. display: block;
  409. background: inherit;
  410. filter: blur(5px);
  411. position: absolute;
  412. width: 100%;
  413. height: 100%;
  414. top: 10rpx;
  415. left: 10rpx;
  416. z-index: -1;
  417. opacity: 0.3;
  418. transform-origin: 0 0;
  419. transform: scale(1, 1);
  420. }
  421. .swiper-item.cur .ui-swiper-main {
  422. transform: scale(1);
  423. transition: all 0.2s ease-in 0s;
  424. opacity: 1;
  425. }
  426. .ui-swiper-dot {
  427. display: none;
  428. }
  429. }
  430. &.hotelDetail {
  431. .swiper-item {
  432. width: 690rpx !important;
  433. left: 30rpx;
  434. box-sizing: border-box;
  435. padding: 20rpx 0rpx;
  436. overflow: initial;
  437. }
  438. .swiper-item .ui-swiper-main {
  439. width: 100%;
  440. display: block;
  441. height: 100%;
  442. transform: scale(0.96);
  443. transition: all 0.2s ease-in 0s;
  444. position: relative;
  445. background-size: cover;
  446. .swiper-image {
  447. height: 100%;
  448. }
  449. }
  450. .swiper-item.cur .ui-swiper-main {
  451. transform: scale(0.96);
  452. transition: all 0.2s ease-in 0s;
  453. }
  454. }
  455. }
  456. </style>