vue实现轮播展示
使用 Vue 实现轮播展示
基于 Swiper 插件实现
安装 Swiper 依赖:
npm install swiper
在 Vue 组件中引入 Swiper:

<template>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide" v-for="(item, index) in slides" :key="index">
<img :src="item.image" :alt="item.title">
</div>
</div>
<div class="swiper-pagination"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
</div>
</template>
<script>
import Swiper from 'swiper'
import 'swiper/css/swiper.min.css'
export default {
data() {
return {
slides: [
{ image: 'image1.jpg', title: 'Slide 1' },
{ image: 'image2.jpg', title: 'Slide 2' },
{ image: 'image3.jpg', title: 'Slide 3' }
]
}
},
mounted() {
new Swiper('.swiper-container', {
loop: true,
autoplay: {
delay: 3000,
disableOnInteraction: false
},
pagination: {
el: '.swiper-pagination',
clickable: true
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev'
}
})
}
}
</script>
<style>
.swiper-container {
width: 100%;
height: 400px;
}
.swiper-slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
</style>
纯 Vue 实现轮播
不使用第三方库的简单实现:
<template>
<div class="carousel">
<div class="slides" :style="slideStyle">
<div class="slide" v-for="(item, index) in slides" :key="index">
<img :src="item.image" :alt="item.title">
</div>
</div>
<button class="prev" @click="prevSlide">‹</button>
<button class="next" @click="nextSlide">›</button>
<div class="dots">
<span
v-for="(dot, index) in slides"
:key="index"
:class="{ active: currentIndex === index }"
@click="goToSlide(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' }
]
}
},
computed: {
slideStyle() {
return {
transform: `translateX(-${this.currentIndex * 100}%)`
}
}
},
methods: {
nextSlide() {
this.currentIndex = (this.currentIndex + 1) % this.slides.length
},
prevSlide() {
this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length
},
goToSlide(index) {
this.currentIndex = index
}
},
mounted() {
setInterval(this.nextSlide, 3000)
}
}
</script>
<style>
.carousel {
position: relative;
width: 100%;
height: 400px;
overflow: hidden;
}
.slides {
display: flex;
height: 100%;
transition: transform 0.5s ease;
}
.slide {
min-width: 100%;
}
.slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
.prev, .next {
position: absolute;
top: 50%;
transform: translateY(-50%);
background: rgba(0,0,0,0.5);
color: white;
border: none;
padding: 10px;
cursor: pointer;
z-index: 10;
}
.prev {
left: 10px;
}
.next {
right: 10px;
}
.dots {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
}
.dots span {
width: 10px;
height: 10px;
margin: 0 5px;
border-radius: 50%;
background: #ccc;
cursor: pointer;
}
.dots span.active {
background: #333;
}
</style>
使用 Vue-Awesome-Swiper 组件
安装依赖:

npm install vue-awesome-swiper swiper
组件实现:
<template>
<swiper :options="swiperOption" ref="mySwiper">
<swiper-slide v-for="(item, index) in slides" :key="index">
<img :src="item.image" :alt="item.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: 'Slide 1' },
{ image: 'image2.jpg', title: 'Slide 2' },
{ image: 'image3.jpg', title: 'Slide 3' }
],
swiperOption: {
loop: true,
autoplay: {
delay: 3000,
disableOnInteraction: false
},
pagination: {
el: '.swiper-pagination',
clickable: true
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev'
}
}
}
}
}
</script>
<style>
.swiper-container {
width: 100%;
height: 400px;
}
.swiper-slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
</style>
响应式轮播实现
在 Swiper 配置中添加响应式断点:
swiperOption: {
// ...其他配置
breakpoints: {
640: {
slidesPerView: 1
},
768: {
slidesPerView: 2
},
1024: {
slidesPerView: 3
}
}
}






