vue如何实现全屏轮播
Vue实现全屏轮播的方法
使用第三方库(如swiper.js)
安装swiper.js库:
npm install swiper
在Vue组件中引入并配置:
<template>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide" v-for="(slide, index) in slides" :key="index">
<img :src="slide.image" alt="">
</div>
</div>
<div class="swiper-pagination"></div>
</div>
</template>
<script>
import Swiper from 'swiper'
import 'swiper/swiper-bundle.css'
export default {
data() {
return {
slides: [
{ image: 'image1.jpg' },
{ image: 'image2.jpg' },
{ image: 'image3.jpg' }
]
}
},
mounted() {
new Swiper('.swiper-container', {
direction: 'vertical',
loop: true,
pagination: {
el: '.swiper-pagination',
clickable: true
},
autoplay: {
delay: 3000
}
})
}
}
</script>
<style scoped>
.swiper-container {
width: 100%;
height: 100vh;
}
.swiper-slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
</style>
纯Vue实现
通过Vue的过渡效果和定时器实现:

<template>
<div class="fullscreen-carousel">
<transition :name="transitionName">
<div
class="slide"
v-for="(slide, index) in slides"
v-show="currentIndex === index"
:key="index"
>
<img :src="slide.image" alt="">
</div>
</transition>
<div class="indicators">
<span
v-for="(slide, index) in slides"
:key="index"
:class="{ active: currentIndex === index }"
@click="goToSlide(index)"
></span>
</div>
</div>
</template>
<script>
export default {
data() {
return {
slides: [
{ image: 'image1.jpg' },
{ image: 'image2.jpg' },
{ image: 'image3.jpg' }
],
currentIndex: 0,
transitionName: 'slide-left',
timer: null
}
},
methods: {
goToSlide(index) {
this.transitionName = index > this.currentIndex ? 'slide-left' : 'slide-right'
this.currentIndex = index
this.resetTimer()
},
nextSlide() {
this.transitionName = 'slide-left'
this.currentIndex = (this.currentIndex + 1) % this.slides.length
},
resetTimer() {
clearInterval(this.timer)
this.startTimer()
},
startTimer() {
this.timer = setInterval(this.nextSlide, 3000)
}
},
mounted() {
this.startTimer()
},
beforeDestroy() {
clearInterval(this.timer)
}
}
</script>
<style scoped>
.fullscreen-carousel {
position: relative;
width: 100%;
height: 100vh;
overflow: hidden;
}
.slide {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
.indicators {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
}
.indicators span {
width: 10px;
height: 10px;
margin: 0 5px;
border-radius: 50%;
background: rgba(255,255,255,0.5);
cursor: pointer;
}
.indicators span.active {
background: white;
}
.slide-left-enter-active,
.slide-left-leave-active,
.slide-right-enter-active,
.slide-right-leave-active {
transition: all 1s ease;
}
.slide-left-enter {
transform: translateX(100%);
}
.slide-left-leave-to {
transform: translateX(-100%);
}
.slide-right-enter {
transform: translateX(-100%);
}
.slide-right-leave-to {
transform: translateX(100%);
}
</style>
关键点说明
确保轮播容器设置为全屏高度:
.container {
height: 100vh;
}
图片适应全屏:

img {
object-fit: cover;
width: 100%;
height: 100%;
}
实现平滑过渡效果:
transition: all 0.5s ease;
自动轮播逻辑:
setInterval(() => {
this.currentIndex = (this.currentIndex + 1) % this.slides.length
}, 3000)
注意事项
- 移动端适配需要添加viewport meta标签
- 图片预加载可以提升用户体验
- 离开页面时清除定时器防止内存泄漏
- 考虑添加触摸滑动支持
- 可以添加键盘事件监听左右箭头切换





