当前位置:   article > 正文

vue3实现图片跟视频混着轮播

vue3实现图片跟视频混着轮播
  1. <div
  2. style="height: 100%; display: flex"
  3. class="carousels"
  4. :style="`width:${widthas}px`"
  5. >
  6. <div
  7. v-for="(item, index) in carousel"
  8. :key="index"
  9. :class="`center ${clasA}`"
  10. :style="`width:${widtha}px;margin-left:${
  11. index == 0 ? marginL : '0px'
  12. }`"
  13. >
  14. <img
  15. v-if="item.type === 'img'"
  16. :src="item.content"
  17. alt=""
  18. style="width: 100%; height: 100%; object-fit: contain"
  19. />
  20. <video
  21. v-else
  22. :src="item.content"
  23. style="width: 100%; height: 100%; object-fit: contain"
  24. controls
  25. ></video>
  26. </div>
  1. const carousel = ref<
  2. {
  3. type: string;
  4. content: string;
  5. time: number;
  6. }[]
  7. >([
  8. {
  9. type: 'img',
  10. content:
  11. 'https://img.zcool.cn/community/01a3ec5c2eee61a80121df90c10c9b.jpg@1280w_1l_2o_100sh.jpg',
  12. time: 2000,
  13. },
  14. {
  15. type: 'img',
  16. content: 'https://pic3.zhimg.com/v2-a5ce347e0efb63a27b67c6ab46e59e12_r.jpg',
  17. time: 2000,
  18. },
  19. {
  20. type: 'img',
  21. content:
  22. 'https://img.zcool.cn/community/0180b26248f6f50002c4212c521a70.jpg?x-oss-process=image/auto-orient,1/resize,m_lfit,w_1280,limit_1/sharpen,100',
  23. time: 2000,
  24. },
  25. {
  26. type: 'img',
  27. content: 'https://img.zcool.cn/community/02wacol7aokntr9snsdiyh3833.jpg',
  28. time: 2000,
  29. },
  30. {
  31. type: 'video',
  32. content: mi1474df5w5snjkh,
  33. time: 4000,
  34. },
  35. {
  36. type: 'img',
  37. content:
  38. 'https://img.zcool.cn/community/01a3ec5c2eee61a80121df90c10c9b.jpg@1280w_1l_2o_100sh.jpg',
  39. time: 2000,
  40. },
  41. ]);
  1. const widthas = ref(0);
  2. const widtha = ref(0);
  3. const cast = () => {
  4. let Dom = document
  5. .getElementsByClassName('slideshow')[0]
  6. .getBoundingClientRect();
  7. let a = Dom.width;
  8. widtha.value = a;
  9. let b = 0;
  10. let c = document.getElementsByClassName('single');
  11. for (let i = 0; i < c.length; i++) {
  12. b += a;
  13. }
  14. widthas.value = a * carousel.value.length;
  15. loginout(a, 0);
  16. };
  17. setTimeout(() => {
  18. cast();
  19. }, 1000);
  20. const marginL = ref('0px');
  21. function loginout(wid: number, num: number) {
  22. let data = carousel.value[num].time + 1000;
  23. clasA.value = 'a';
  24. if (num == carousel.value.length - 1) {
  25. clasA.value = 'single';
  26. marginL.value = -wid * num + 'px';
  27. data = 1000;
  28. } else if (num == 0) {
  29. marginL.value = 0 + 'px';
  30. data = carousel.value[num].time;
  31. } else {
  32. clasA.value = 'single';
  33. marginL.value = -wid * num + 'px';
  34. }
  35. setTimeout(() => {
  36. if (num + 1 >= carousel.value.length) {
  37. num = 0;
  38. } else {
  39. num = num + 1;
  40. }
  41. loginout(wid, num);
  42. }, data);
  43. }
  1. .single {
  2. transition: all 1s ease-in-out;
  3. }

唯一的缺点就是:切换的时候非常卡顿,因为使用的是transition不是animation

尝试方案:1:使用animation

结果:动画只会在第一次动的时候生效不生效,如果次数设置为无线的话就会一直来回切换

2:使用多个动画

结果:每个div都分配到了动画但是统一时间执行完(中间夹杂着视频没办法动态延缓执行时间)

如果有哪位解决了麻烦说下

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/966486
推荐阅读
相关标签
  

闽ICP备14008679号