英文:
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 '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);
/* 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:
<SwiperSlide>
<video width="900" height="800" autoPlay muted> // removed loop
<source src={ship} type='video/mp4'/>
</video>
</SwiperSlide>
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论