Swiper轮播对于视频轮播不按预期工作。

huangapple go评论70阅读模式
英文:

Swiper loop not working as intended for video carousel

问题

我正在创建一个视频轮播,每当进度条到达末尾时,视频循环播放,视频本身播放正常,但 swiper 中的循环属性似乎无法正常工作。以下是代码:

import { useRef, useEffect } from 'react';
import { Pagination, A11y, type Swiper as SwiperRef } from 'swiper';
import { Swiper, SwiperSlide } from 'swiper/react';

import ship from '../assets/videos/ship.mp4';
import highway from '../assets/videos/pexels-kelly-lacy-5473765.mp4';

import 'swiper/css';
import 'swiper/css/pagination';

const NewHome = () => {
  return (
    <Swiper
      modules={[Pagination, A11y]}
      autoplay={{ delay: 5000 }}
      pagination={{ clickable: true }}
      loop
      className="w-1/2"
    >
      <SwiperSlide>
        <video width="900" height="800" autoPlay muted loop>
          <source src={ship} type='video/mp4'/>
        </video>
      </SwiperSlide>
      <SwiperSlide>
        <video src={highway} width="900" height="800"/>
      </SwiperSlide>
    </Swiper>
  )
}

export default NewHome;

CSS

.swiper-container {
  width: 100%;
  height: 200px;
}
.swiper-container + .swiper-container {
  margin-top: 30px;
}

.swiper-slide {
  text-align: center;
  font-size: 18px;
  background: #fff;
  /* Center slide text vertically */
  display: flex;
  justify-content: center;
  align-items: center;
}

.swiper-pagination-bullet {
  width: 10rem;
  height: 2px;
  border-radius: 0;
  position: relative;
  overflow: hidden;
}
.swiper-pagination-bullet::before {
  content: "";
  display: block;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}

.swiper-pagination-bullet-active {
  background: rgba(0, 0, 0, 0.4);
}
.swiper-pagination-bullet-active::before {
  background-color: red;
  -webkit-animation: slide-progress 2s cubic-bezier(0.3, 0, 0.3, 1) forwards;
  animation: slide-progress 2s cubic-bezier(0.3, 0, 0.3, 1) forwards;
}
.swiper-paused .swiper-pagination-bullet-active::before {
  -webkit-animation-play-state: paused;
  animation-play-state: paused;
}

@-webkit-keyframes slide-progress {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(0);
  }
}

@keyframes slide-progress {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(0);
  }
}

我尝试向 autoplay 添加了 delay 选项,但它似乎仍然无法正常工作,我期望在每个幻灯片的进度条到达末尾后切换到下一个视频。

英文:

I'm making a video carousel which loops each time it progressive bar reaches it end,the video plays perfectly, but the loop attribute in swiper doesn't seem to work, Here is the code

import { useRef, useEffect } from &#39;react&#39;;
import { Pagination, A11y, type Swiper as SwiperRef } from &#39;swiper&#39;;
import { Swiper, SwiperSlide } from &#39;swiper/react&#39;;
import ship from &#39;../assets/videos/ship.mp4&#39;
import highway from &#39;../assets/videos/pexels-kelly-lacy-5473765.mp4&#39;
import &#39;swiper/css&#39;;
import &#39;swiper/css/pagination&#39;;
const NewHome = () =&gt; {
return (
&lt;Swiper
modules={[ Pagination , A11y]}
autoplay={{ delay: 5000}}
pagination={{ clickable: true }}
loop
className=&quot;w-1/2&quot;
&gt;
&lt;SwiperSlide&gt;
&lt;video width=&quot;900&quot; height=&quot;800&quot; autoPlay muted loop&gt;
&lt;source src={ship} type=&#39;video/mp4&#39;/&gt;
&lt;/video&gt;
&lt;/SwiperSlide&gt;
&lt;SwiperSlide&gt;
&lt;video src={highway} width=&quot;900&quot; height=&quot;800&quot;/&gt;
&lt;/SwiperSlide&gt;
&lt;/Swiper&gt;
)
}
export default NewHome

CSS

.swiper-container {
width: 100%;
height: 200px;
}
.swiper-container + .swiper-container {
margin-top: 30px;
}
.swiper-slide {
text-align: center;
font-size: 18px;
background: #fff;
/* Center slide text vertically */
display: flex;
justify-content: center;
align-items: center;
}
.swiper-pagination-bullet {
width: 10rem;
height: 2px;
border-radius: 0;
position: relative;
overflow: hidden;
}
.swiper-pagination-bullet::before {
content: &quot;&quot;;
display: block;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
.swiper-pagination-bullet-active {
background: rgba(0, 0, 0, 0.4);
/* outline: 1px red solid; */
}
.swiper-pagination-bullet-active::before {
background-color: red;
-webkit-animation: slide-progress 2s cubic-bezier(0.3, 0, 0.3, 1) forwards;
animation: slide-progress 2s cubic-bezier(0.3, 0, 0.3, 1) forwards;
}
.swiper-paused .swiper-pagination-bullet-active::before {
-webkit-animation-play-state: paused;
animation-play-state: paused;
}
@-webkit-keyframes slide-progress {
0% {
transform: translateX(-100%);
}
100% {
transform: translateX(0);
}
}
@keyframes slide-progress {
0% {
transform: translateX(-100%);
}
100% {
transform: translateX(0);
}
}

I tried adding a delay option to the autoplay but its still didn't work, I expect it switch to the next video after the progress bar of each slide reaches it end

答案1

得分: 0

Swiper 组件的 loop 属性可能无法正常工作,因为视频拥有自己的 loop 属性。如果视频循环播放,它们将无法触发 Swiper 组件移动到下一张幻灯片。

尝试从 video 中移除循环,依赖 Swiper 组件的循环功能:

<SwiperSlide>
<video width="900" height="800" autoPlay muted> // 移除了 loop
<source src={ship} type='video/mp4'/>
</video>
</SwiperSlide>
英文:

The loop property of the Swiper component may not be working because the videos have their own loop attribute.
If you loop your videos they cannot trigger the Swiper component to move to the next slide.

Try removing loop from video and rely on Swiper component's loop:

&lt;SwiperSlide&gt;
&lt;video width=&quot;900&quot; height=&quot;800&quot; autoPlay muted&gt; // removed loop
&lt;source src={ship} type=&#39;video/mp4&#39;/&gt;
&lt;/video&gt;
&lt;/SwiperSlide&gt;

答案2

得分: 0

找到解决方案,所以我忘记从swiper中导入Autoplay模块,现在它可以正常工作

注意:
为了使其正常工作,您还需要导入autoplay模块的CSS文件

英文:

Found the solution, so I forgot to import the Autoplay module from swiper and now it works perfectly fine

Note:
You'll also need to import the css file for the autoplay module in order to make it work

huangapple
  • 本文由 发表于 2023年2月6日 17:45:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定