vue前端实现轮播图
使用 Vue 和第三方库实现轮播图
推荐使用 vue-awesome-swiper 库,它是基于 Swiper 的 Vue 封装,功能强大且易于集成。
安装依赖:
npm install swiper vue-awesome-swiper
基础实现代码:

<template>
<swiper :options="swiperOption">
<swiper-slide v-for="(slide, index) in slides" :key="index">
<img :src="slide.image" alt="">
</swiper-slide>
<div class="swiper-pagination" slot="pagination"></div>
</swiper>
</template>
<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'
export default {
components: {
Swiper,
SwiperSlide
},
data() {
return {
slides: [
{ image: 'image1.jpg' },
{ image: 'image2.jpg' },
{ image: 'image3.jpg' }
],
swiperOption: {
pagination: {
el: '.swiper-pagination'
},
loop: true,
autoplay: {
delay: 3000
}
}
}
}
}
</script>
纯 Vue 实现简易轮播图
如果不希望引入第三方库,可以使用纯 Vue 实现基础功能:
<template>
<div class="carousel">
<div class="slides" :style="{ transform: `translateX(-${currentIndex * 100}%)` }">
<div v-for="(slide, index) in slides" :key="index" class="slide">
<img :src="slide.image" alt="">
</div>
</div>
<button @click="prev">Prev</button>
<button @click="next">Next</button>
</div>
</template>
<script>
export default {
data() {
return {
currentIndex: 0,
slides: [
{ image: 'image1.jpg' },
{ image: 'image2.jpg' },
{ image: 'image3.jpg' }
]
}
},
methods: {
next() {
this.currentIndex = (this.currentIndex + 1) % this.slides.length
},
prev() {
this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length
}
},
mounted() {
setInterval(this.next, 3000)
}
}
</script>
<style>
.carousel {
overflow: hidden;
position: relative;
width: 100%;
height: 300px;
}
.slides {
display: flex;
transition: transform 0.5s ease;
}
.slide {
min-width: 100%;
}
</style>
轮播图功能扩展
添加指示器和过渡效果:

<template>
<div class="carousel">
<div class="slides" :style="{ transform: `translateX(-${currentIndex * 100}%)` }">
<!-- 同上 -->
</div>
<div class="indicators">
<span
v-for="(slide, index) in slides"
:key="index"
:class="{ active: currentIndex === index }"
@click="goTo(index)"
></span>
</div>
</div>
</template>
<script>
export default {
// 同上
methods: {
goTo(index) {
this.currentIndex = index
}
}
}
</script>
<style>
.indicators {
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
}
.indicators span {
display: inline-block;
width: 10px;
height: 10px;
margin: 0 5px;
border-radius: 50%;
background: #ccc;
cursor: pointer;
}
.indicators span.active {
background: #333;
}
</style>
响应式轮播图
添加窗口大小变化的响应处理:
export default {
// 同上
mounted() {
window.addEventListener('resize', this.handleResize)
this.handleResize()
},
beforeDestroy() {
window.removeEventListener('resize', this.handleResize)
},
methods: {
handleResize() {
// 根据窗口大小调整轮播图尺寸
this.carouselWidth = this.$el.offsetWidth
}
}
}
性能优化建议
使用 v-if 或 v-show 控制非当前活跃的幻灯片
<template>
<div class="carousel">
<div class="slides">
<div
v-for="(slide, index) in slides"
:key="index"
class="slide"
:class="{ active: currentIndex === index }"
>
<img v-show="currentIndex === index" :src="slide.image" alt="">
</div>
</div>
</div>
</template>
预加载图片资源
export default {
mounted() {
this.preloadImages()
},
methods: {
preloadImages() {
this.slides.forEach(slide => {
const img = new Image()
img.src = slide.image
})
}
}
}






