vue实现轮播图源码
Vue 轮播图实现方法
使用 Vue 3 的 Composition API 和 <transition-group> 实现基础轮播图:
<template>
<div class="carousel-container">
<button @click="prevSlide">上一张</button>
<transition-group name="slide" tag="div" class="slides">
<div v-for="(slide, index) in slides"
:key="index"
v-show="currentIndex === index"
class="slide">
<img :src="slide.image" :alt="slide.title">
</div>
</transition-group>
<button @click="nextSlide">下一张</button>
<div class="dots">
<span v-for="(dot, index) in slides"
:key="index"
@click="goToSlide(index)"
:class="{ active: currentIndex === index }">
</span>
</div>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
const slides = ref([
{ image: 'image1.jpg', title: '图片1' },
{ image: 'image2.jpg', title: '图片2' },
{ image: 'image3.jpg', title: '图片3' }
])
const currentIndex = ref(0)
const autoPlay = ref(true)
const interval = ref(3000)
const nextSlide = () => {
currentIndex.value = (currentIndex.value + 1) % slides.value.length
}
const prevSlide = () => {
currentIndex.value = (currentIndex.value - 1 + slides.value.length) % slides.value.length
}
const goToSlide = (index) => {
currentIndex.value = index
}
let timer
onMounted(() => {
if (autoPlay.value) {
timer = setInterval(nextSlide, interval.value)
}
})
</script>
<style>
.carousel-container {
position: relative;
width: 100%;
max-width: 800px;
margin: 0 auto;
overflow: hidden;
}
.slides {
display: flex;
transition: transform 0.5s ease;
}
.slide {
width: 100%;
flex-shrink: 0;
}
.slide img {
width: 100%;
height: auto;
}
.dots {
text-align: center;
margin-top: 10px;
}
.dots span {
display: inline-block;
width: 12px;
height: 12px;
margin: 0 5px;
border-radius: 50%;
background-color: #ccc;
cursor: pointer;
}
.dots span.active {
background-color: #333;
}
.slide-enter-active, .slide-leave-active {
transition: opacity 0.5s;
}
.slide-enter-from, .slide-leave-to {
opacity: 0;
}
</style>
使用第三方库实现
安装 vue-awesome-swiper 库:

npm install swiper vue-awesome-swiper
组件实现代码:
<template>
<swiper
:options="swiperOptions"
:auto-update="true"
:auto-destroy="true"
@ready="onSwiperReady"
@slideChange="onSlideChange"
>
<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/css/swiper.css'
export default {
components: {
Swiper,
SwiperSlide
},
data() {
return {
slides: [
{ image: 'image1.jpg', title: '图片1' },
{ image: 'image2.jpg', title: '图片2' },
{ image: 'image3.jpg', title: '图片3' }
],
swiperOptions: {
pagination: {
el: '.swiper-pagination',
clickable: true
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev'
},
loop: true,
autoplay: {
delay: 3000,
disableOnInteraction: false
}
}
}
},
methods: {
onSwiperReady(swiper) {
console.log('Swiper ready', swiper)
},
onSlideChange() {
console.log('Slide changed')
}
}
}
</script>
<style>
.swiper-container {
width: 100%;
height: 300px;
}
.swiper-slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
</style>
关键功能扩展
添加淡入淡出动画效果:

.fade-enter-active, .fade-leave-active {
transition: opacity 0.5s;
}
.fade-enter-from, .fade-leave-to {
opacity: 0;
}
实现缩略图导航:
<div class="thumbnails">
<img
v-for="(slide, index) in slides"
:key="index"
:src="slide.image"
@click="goToSlide(index)"
:class="{ active: currentIndex === index }">
</div>
添加触摸滑动支持:
const swiperOptions = {
// ...
touchEventsTarget: 'container',
simulateTouch: true,
touchRatio: 1,
touchAngle: 45,
grabCursor: true
}






