vue实现简单轮播图
Vue 实现简单轮播图
使用 Vue 3 的 Composition API 实现基础轮播图
<template>
<div class="carousel-container">
<div class="carousel" :style="{ transform: `translateX(-${currentIndex * 100}%)` }">
<div class="slide" v-for="(slide, index) in slides" :key="index">
<img :src="slide.image" :alt="slide.alt">
</div>
</div>
<button @click="prevSlide">Previous</button>
<button @click="nextSlide">Next</button>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue';
const slides = ref([
{ image: 'image1.jpg', alt: 'Image 1' },
{ image: 'image2.jpg', alt: 'Image 2' },
{ image: 'image3.jpg', alt: 'Image 3' }
]);
const currentIndex = ref(0);
const nextSlide = () => {
currentIndex.value = (currentIndex.value + 1) % slides.value.length;
};
const prevSlide = () => {
currentIndex.value = (currentIndex.value - 1 + slides.value.length) % slides.value.length;
};
onMounted(() => {
setInterval(nextSlide, 3000);
});
</script>
<style>
.carousel-container {
overflow: hidden;
position: relative;
width: 100%;
}
.carousel {
display: flex;
transition: transform 0.5s ease;
}
.slide {
min-width: 100%;
}
.slide img {
width: 100%;
height: auto;
}
</style>
使用 Vue 2 的 Options API 实现
<template>
<div class="carousel">
<transition-group name="slide" tag="div">
<div v-for="(item, index) in slides" :key="index" v-show="currentSlide === index">
<img :src="item.image" :alt="item.alt">
</div>
</transition-group>
<button @click="prev">Prev</button>
<button @click="next">Next</button>
</div>
</template>
<script>
export default {
data() {
return {
currentSlide: 0,
slides: [
{ image: 'image1.jpg', alt: 'Image 1' },
{ image: 'image2.jpg', alt: 'Image 2' },
{ image: 'image3.jpg', alt: 'Image 3' }
]
};
},
methods: {
next() {
this.currentSlide = (this.currentSlide + 1) % this.slides.length;
},
prev() {
this.currentSlide = (this.currentSlide - 1 + this.slides.length) % this.slides.length;
}
},
mounted() {
setInterval(this.next, 3000);
}
};
</script>
<style>
.carousel {
position: relative;
overflow: hidden;
width: 100%;
}
.slide-enter-active, .slide-leave-active {
transition: all 1s ease;
}
.slide-enter {
transform: translateX(100%);
}
.slide-leave-to {
transform: translateX(-100%);
}
img {
width: 100%;
}
</style>
添加指示器(小圆点导航)
在 Vue 3 版本中添加以下代码:

<div class="indicators">
<span
v-for="(_, index) in slides"
:key="index"
@click="currentIndex = index"
:class="{ active: currentIndex === index }"
></span>
</div>
<style>
.indicators {
display: flex;
justify-content: center;
margin-top: 10px;
}
.indicators span {
width: 12px;
height: 12px;
margin: 0 5px;
border-radius: 50%;
background-color: #ccc;
cursor: pointer;
}
.indicators span.active {
background-color: #333;
}
</style>
实现无限循环效果
修改 Vue 3 的 nextSlide 和 prevSlide 方法:

const nextSlide = () => {
if (currentIndex.value >= slides.value.length - 1) {
currentIndex.value = 0;
} else {
currentIndex.value++;
}
};
const prevSlide = () => {
if (currentIndex.value <= 0) {
currentIndex.value = slides.value.length - 1;
} else {
currentIndex.value--;
}
};
使用第三方库 vue-awesome-swiper
安装:
npm install swiper vue-awesome-swiper
示例代码:
<template>
<swiper :options="swiperOptions">
<swiper-slide v-for="(slide, index) in slides" :key="index">
<img :src="slide.image" :alt="slide.alt">
</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', alt: 'Image 1' },
{ image: 'image2.jpg', alt: 'Image 2' },
{ image: 'image3.jpg', alt: 'Image 3' }
],
swiperOptions: {
pagination: {
el: '.swiper-pagination',
clickable: true
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev'
},
loop: true,
autoplay: {
delay: 3000,
disableOnInteraction: false
}
}
}
}
}
</script>






