vue前端实现轮播图
使用 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="slide.title">
</div>
</div>
<button class="prev" @click="prevSlide">❮</button>
<button class="next" @click="nextSlide">❯</button>
<div class="indicators">
<span
v-for="(slide, index) in slides"
:key="index"
@click="goToSlide(index)"
:class="{ active: currentIndex === index }"
></span>
</div>
</div>
</template>
脚本部分
<script>
export default {
data() {
return {
currentIndex: 0,
slides: [
{ image: 'image1.jpg', title: 'Slide 1' },
{ image: 'image2.jpg', title: 'Slide 2' },
{ image: 'image3.jpg', title: 'Slide 3' }
],
autoPlayInterval: null
}
},
methods: {
prevSlide() {
this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length
},
nextSlide() {
this.currentIndex = (this.currentIndex + 1) % this.slides.length
},
goToSlide(index) {
this.currentIndex = index
},
startAutoPlay() {
this.autoPlayInterval = setInterval(this.nextSlide, 3000)
},
stopAutoPlay() {
clearInterval(this.autoPlayInterval)
}
},
mounted() {
this.startAutoPlay()
},
beforeUnmount() {
this.stopAutoPlay()
}
}
</script>
样式部分
<style scoped>
.carousel {
position: relative;
width: 100%;
overflow: hidden;
}
.slides {
display: flex;
transition: transform 0.5s ease;
}
.slide {
min-width: 100%;
box-sizing: border-box;
}
.slide img {
width: 100%;
height: auto;
}
.prev, .next {
position: absolute;
top: 50%;
transform: translateY(-50%);
cursor: pointer;
padding: 16px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
background-color: rgba(0,0,0,0.5);
border: none;
}
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
.indicators {
text-align: center;
position: absolute;
bottom: 10px;
width: 100%;
}
.indicators span {
display: inline-block;
width: 12px;
height: 12px;
margin: 0 4px;
background-color: #bbb;
border-radius: 50%;
cursor: pointer;
}
.indicators span.active {
background-color: #717171;
}
</style>
使用 Vue 轮播图库
安装 Vue-Awesome-Swiper
npm install swiper vue-awesome-swiper --save
组件实现

<template>
<swiper :options="swiperOption" ref="mySwiper">
<swiper-slide v-for="(slide, index) in slides" :key="index">
<img :src="slide.image" :alt="slide.title">
</swiper-slide>
<div class="swiper-pagination" slot="pagination"></div>
<div class="swiper-button-prev" slot="button-prev"></div>
<div class="swiper-button-next" slot="button-next"></div>
</swiper>
</template>
<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
import 'swiper/swiper-bundle.css'
export default {
components: {
Swiper,
SwiperSlide
},
data() {
return {
slides: [
{ image: 'image1.jpg', title: 'Slide 1' },
{ image: 'image2.jpg', title: 'Slide 2' },
{ image: 'image3.jpg', title: 'Slide 3' }
],
swiperOption: {
pagination: {
el: '.swiper-pagination',
clickable: true
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev'
},
autoplay: {
delay: 3000,
disableOnInteraction: false
},
loop: true
}
}
}
}
</script>
关键实现要点
动画效果处理 CSS transition 属性实现平滑滑动效果
.slides {
transition: transform 0.5s ease;
}
无限循环实现 通过取模运算实现无限循环
this.currentIndex = (this.currentIndex + 1) % this.slides.length
响应式设计 确保轮播图在不同屏幕尺寸下正常显示

.slide img {
width: 100%;
height: auto;
max-height: 500px;
object-fit: cover;
}
触摸事件支持 使用第三方库如Swiper可自动支持触摸滑动
性能优化 图片懒加载实现
swiperOption: {
lazy: {
loadPrevNext: true
}
}
自动播放控制 组件挂载时启动定时器,销毁时清除
mounted() {
this.startAutoPlay()
},
beforeUnmount() {
this.stopAutoPlay()
}






