su-swiper.vue 11 KB

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