su-swiper.vue 12 KB

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