su-swiper.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. // 数据
  90. const state = reactive({
  91. imgHeight: 0,
  92. cur: 0,
  93. moveX: 0,
  94. videoPlaySataus: false,
  95. heightList: [],
  96. });
  97. const refs = reactive({
  98. videoRef: {},
  99. });
  100. // 接收参数
  101. const props = defineProps({
  102. circular: {
  103. type: Boolean,
  104. default: true,
  105. },
  106. autoplay: {
  107. type: Boolean,
  108. default: false,
  109. },
  110. interval: {
  111. type: Number,
  112. default: 5000,
  113. },
  114. duration: {
  115. type: Number,
  116. default: 500,
  117. },
  118. mode: {
  119. type: String,
  120. default: 'default',
  121. },
  122. imageMode: {
  123. type: String,
  124. default: 'scaleToFill',
  125. },
  126. list: {
  127. type: Array,
  128. default() {
  129. return [];
  130. },
  131. },
  132. dotStyle: {
  133. type: String,
  134. default: 'long', //default long tag
  135. },
  136. dotCur: {
  137. type: String,
  138. default: 'ss-bg-opactity-block',
  139. },
  140. bg: {
  141. type: String,
  142. default: 'bg-none',
  143. },
  144. height: {
  145. type: Number,
  146. default: 0,
  147. },
  148. imgHeight: {
  149. type: Number,
  150. default: 0,
  151. },
  152. imgTopRadius: {
  153. type: Number,
  154. default: 0,
  155. },
  156. imgBottomRadius: {
  157. type: Number,
  158. default: 0,
  159. },
  160. isPreview: {
  161. type: Boolean,
  162. default: false,
  163. },
  164. seizeHeight: {
  165. type: Number,
  166. default: 200,
  167. },
  168. });
  169. // current 改变时会触发 change 事件
  170. const swiperChange = (e) => {
  171. if (e.detail.source !== 'touch' && e.detail.source !== 'autoplay') return;
  172. state.cur = e.detail.current;
  173. state.videoPlaySataus = false;
  174. if (props.list[state.cur].type === 'video') {
  175. refs.videoRef[`video_${state.cur}`].pausePlay();
  176. }
  177. };
  178. // 点击轮播组件
  179. const onSwiperItem = (item) => {
  180. if (item.type === 'video') {
  181. state.videoPlaySataus = true;
  182. } else {
  183. sheep.$router.go(item.url);
  184. onPreview();
  185. }
  186. };
  187. const onPreview = () => {
  188. if (!props.isPreview) return;
  189. props.list.splice(
  190. props.list.findIndex((item) => item.type === 'video'),
  191. 1,
  192. );
  193. let previewImage = props.list;
  194. uni.previewImage({
  195. urls:
  196. previewImage.length < 1
  197. ? [props.src]
  198. : previewImage.reduce((pre, cur) => {
  199. pre.push(cur.src);
  200. return pre;
  201. }, []),
  202. current: state.cur,
  203. // longPressActions: {
  204. // itemList: ['发送给朋友', '保存图片', '收藏'],
  205. // success: function (data) {
  206. // console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
  207. // },
  208. // fail: function (err) {
  209. // console.log(err.errMsg);
  210. // },
  211. // },
  212. });
  213. };
  214. //
  215. // swiper-item 的位置发生改变时会触发 transition
  216. const transition = (e) => {
  217. // #ifdef APP-PLUS
  218. state.moveX = e.detail.dx;
  219. // #endif
  220. };
  221. // 动画结束时会触发 animationfinish
  222. const animationfinish = (e) => {
  223. state.moveX = 0;
  224. };
  225. const videoTimeupdate = (e) => {
  226. props.list[state.cur].currentTime = e.detail.currentTime;
  227. };
  228. // 自动计算高度
  229. const customStyle = computed(() => {
  230. let height;
  231. // 固定高度情况
  232. if (props.height !== 0) {
  233. height = props.height;
  234. }
  235. // 自动高度情况
  236. if (props.height === 0) {
  237. // 图片预加载占位高度
  238. if (state.imgHeight !== 0) {
  239. height = state.imgHeight;
  240. } else if (props.seizeHeight !== 0) {
  241. height = props.seizeHeight;
  242. }
  243. }
  244. return {
  245. height: height + 'rpx',
  246. };
  247. });
  248. // 计算轮播图片最大高度
  249. function onImgLoad(e) {
  250. if (props.height === 0) {
  251. let newHeight = (e.detail.height / e.detail.width) * 750;
  252. if (state.imgHeight < newHeight) {
  253. state.imgHeight = newHeight;
  254. }
  255. }
  256. }
  257. </script>
  258. <style lang="scss" scoped>
  259. .ui-swiper {
  260. position: relative;
  261. .ui-swiper-main {
  262. width: 100%;
  263. height: 100%;
  264. }
  265. .ui-swiper-main .swiper-image {
  266. width: 100%;
  267. height: 100%;
  268. }
  269. .ui-swiper-dot {
  270. position: absolute;
  271. width: 100%;
  272. bottom: 20rpx;
  273. height: 30rpx;
  274. display: flex;
  275. align-items: center;
  276. justify-content: center;
  277. &.default .line-box {
  278. display: inline-flex;
  279. border-radius: 50rpx;
  280. width: 6px;
  281. height: 6px;
  282. border: 2px solid transparent;
  283. margin: 0 10rpx;
  284. opacity: 0.3;
  285. position: relative;
  286. justify-content: center;
  287. align-items: center;
  288. &.cur {
  289. width: 8px;
  290. height: 8px;
  291. opacity: 1;
  292. border: 0px solid transparent;
  293. }
  294. &.cur::after {
  295. content: '';
  296. border-radius: 50rpx;
  297. width: 4px;
  298. height: 4px;
  299. background-color: #fff;
  300. }
  301. }
  302. &.long .line-box {
  303. display: inline-block;
  304. border-radius: 100rpx;
  305. width: 6px;
  306. height: 6px;
  307. margin: 0 10rpx;
  308. opacity: 0.3;
  309. position: relative;
  310. &.cur {
  311. width: 24rpx;
  312. opacity: 1;
  313. }
  314. &.cur::after {
  315. }
  316. }
  317. &.line {
  318. bottom: 20rpx;
  319. .line-box {
  320. display: inline-block;
  321. width: 30px;
  322. height: 3px;
  323. opacity: 0.3;
  324. position: relative;
  325. &.cur {
  326. opacity: 1;
  327. }
  328. }
  329. }
  330. &.tag {
  331. justify-content: flex-end;
  332. position: absolute;
  333. bottom: 20rpx;
  334. right: 20rpx;
  335. }
  336. }
  337. &.card {
  338. .swiper-item {
  339. width: 610rpx !important;
  340. left: 70rpx;
  341. box-sizing: border-box;
  342. padding: 20rpx 0rpx 60rpx;
  343. overflow: initial;
  344. }
  345. .swiper-item .ui-swiper-main {
  346. width: 100%;
  347. display: block;
  348. height: 100%;
  349. transform: scale(0.9);
  350. transition: all 0.2s ease-in 0s;
  351. position: relative;
  352. background-size: cover;
  353. .swiper-image {
  354. height: 100%;
  355. }
  356. }
  357. .swiper-item .ui-swiper-main::before {
  358. content: '';
  359. display: block;
  360. background: inherit;
  361. filter: blur(5px);
  362. position: absolute;
  363. width: 100%;
  364. height: 100%;
  365. top: 10rpx;
  366. left: 10rpx;
  367. z-index: -1;
  368. opacity: 0.3;
  369. transform-origin: 0 0;
  370. transform: scale(1, 1);
  371. }
  372. .swiper-item.cur .ui-swiper-main {
  373. transform: scale(1);
  374. transition: all 0.2s ease-in 0s;
  375. }
  376. .ui-swiper-dot.tag {
  377. position: absolute;
  378. bottom: 85rpx;
  379. right: 75rpx;
  380. }
  381. }
  382. &.hotelCard {
  383. .swiper-item {
  384. width: 650rpx !important;
  385. left: 30rpx;
  386. box-sizing: border-box;
  387. padding: 0rpx 0rpx 50rpx;
  388. overflow: initial;
  389. }
  390. .swiper-item .ui-swiper-main {
  391. width: 100%;
  392. display: block;
  393. height: 100%;
  394. transform: scale(0.9);
  395. opacity: 0.8;
  396. transition: all 0.2s ease-in 0s;
  397. position: relative;
  398. background-size: cover;
  399. .swiper-image {
  400. width: 100%;
  401. height: 400rpx;
  402. }
  403. }
  404. .swiper-item .ui-swiper-main::before {
  405. content: '';
  406. display: block;
  407. background: inherit;
  408. filter: blur(5px);
  409. position: absolute;
  410. width: 100%;
  411. height: 100%;
  412. top: 10rpx;
  413. left: 10rpx;
  414. z-index: -1;
  415. opacity: 0.3;
  416. transform-origin: 0 0;
  417. transform: scale(1, 1);
  418. }
  419. .swiper-item.cur .ui-swiper-main {
  420. transform: scale(1);
  421. transition: all 0.2s ease-in 0s;
  422. opacity: 1;
  423. }
  424. .ui-swiper-dot {
  425. display: none;
  426. }
  427. }
  428. &.hotelDetail {
  429. .swiper-item {
  430. width: 690rpx !important;
  431. left: 30rpx;
  432. box-sizing: border-box;
  433. padding: 20rpx 0rpx;
  434. overflow: initial;
  435. }
  436. .swiper-item .ui-swiper-main {
  437. width: 100%;
  438. display: block;
  439. height: 100%;
  440. transform: scale(0.96);
  441. transition: all 0.2s ease-in 0s;
  442. position: relative;
  443. background-size: cover;
  444. .swiper-image {
  445. height: 100%;
  446. }
  447. }
  448. .swiper-item.cur .ui-swiper-main {
  449. transform: scale(0.96);
  450. transition: all 0.2s ease-in 0s;
  451. }
  452. }
  453. }
  454. </style>