su-swiper.vue 10 KB

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