轮播图vue实现
使用 Vue 实现轮播图
基础实现(基于 v-for 和 v-show)
通过 Vue 的指令和响应式数据控制轮播图的显示与切换。
<template>
<div class="carousel">
<div class="carousel-item" v-for="(item, index) in items" :key="index" v-show="currentIndex === index">
<img :src="item.image" :alt="item.title">
<div class="title">{{ item.title }}</div>
</div>
<button @click="prev">Prev</button>
<button @click="next">Next</button>
</div>
</template>
<script>
export default {
data() {
return {
currentIndex: 0,
items: [
{ image: 'image1.jpg', title: 'Slide 1' },
{ image: 'image2.jpg', title: 'Slide 2' },
{ image: 'image3.jpg', title: 'Slide 3' }
]
}
},
methods: {
prev() {
this.currentIndex = (this.currentIndex - 1 + this.items.length) % this.items.length;
},
next() {
this.currentIndex = (this.currentIndex + 1) % this.items.length;
}
}
}
</script>
<style>
.carousel {
position: relative;
width: 100%;
height: 300px;
overflow: hidden;
}
.carousel-item {
position: absolute;
width: 100%;
height: 100%;
transition: opacity 0.5s ease;
}
</style>
自动轮播功能
添加定时器实现自动轮播,并在组件销毁时清除定时器以避免内存泄漏。
export default {
data() {
return {
currentIndex: 0,
items: [...],
interval: null
}
},
mounted() {
this.startAutoPlay();
},
beforeDestroy() {
this.stopAutoPlay();
},
methods: {
startAutoPlay() {
this.interval = setInterval(() => {
this.next();
}, 3000);
},
stopAutoPlay() {
if (this.interval) {
clearInterval(this.interval);
}
}
}
}
使用第三方库(如 Swiper)
对于更复杂的需求(如手势滑动、无缝循环),可以集成 Swiper 库。

安装 Swiper:
npm install swiper vue-awesome-swiper
示例代码:

<template>
<swiper :options="swiperOptions">
<swiper-slide v-for="(item, index) in items" :key="index">
<img :src="item.image" :alt="item.title">
</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 {
items: [...],
swiperOptions: {
pagination: { el: '.swiper-pagination' },
loop: true,
autoplay: { delay: 3000 }
}
}
}
}
</script>
过渡动画效果
通过 Vue 的 <transition> 组件实现平滑的切换动画。
<template>
<div class="carousel">
<transition :name="transitionName">
<div class="carousel-item" v-for="(item, index) in items" :key="index" v-show="currentIndex === index">
<img :src="item.image">
</div>
</transition>
</div>
</template>
<script>
export default {
data() {
return {
transitionName: 'slide-left',
currentIndex: 0
}
},
methods: {
next() {
this.transitionName = 'slide-left';
this.currentIndex = (this.currentIndex + 1) % this.items.length;
},
prev() {
this.transitionName = 'slide-right';
this.currentIndex = (this.currentIndex - 1 + this.items.length) % this.items.length;
}
}
}
</script>
<style>
.slide-left-enter-active, .slide-left-leave-active,
.slide-right-enter-active, .slide-right-leave-active {
transition: transform 0.5s 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>
响应式设计
通过计算属性或 CSS 媒体查询确保轮播图在不同设备上正常显示。
@media (max-width: 768px) {
.carousel {
height: 200px;
}
}
指示器和导航按钮
添加分页指示器和自定义导航按钮增强用户体验。
<template>
<div class="carousel">
<!-- 轮播项 -->
<div class="indicators">
<span v-for="(item, index) in items"
:key="index"
:class="{ active: currentIndex === index }"
@click="currentIndex = index">
</span>
</div>
</div>
</template>
<style>
.indicators {
display: flex;
justify-content: center;
margin-top: 10px;
}
.indicators span {
width: 10px;
height: 10px;
margin: 0 5px;
border-radius: 50%;
background: #ccc;
cursor: pointer;
}
.indicators span.active {
background: #333;
}
</style>






