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 images" :key="index">
<img :src="item" alt="">
</div>
</div>
<div class="swiper-pagination"></div>
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
</div>
</template>
<script>
import Swiper from 'swiper'
import 'swiper/css/swiper.min.css'
export default {
data() {
return {
images: [
'image1.jpg',
'image2.jpg',
'image3.jpg'
]
}
},
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 scoped>
.swiper-container {
width: 100%;
height: 300px;
}
.swiper-slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
</style>
纯 CSS 实现简单轮播
利用 CSS 动画实现基础轮播效果:

<template>
<div class="carousel">
<div class="slides" :style="{ transform: `translateX(-${currentIndex * 100}%)` }">
<div class="slide" v-for="(image, index) in images" :key="index">
<img :src="image" alt="">
</div>
</div>
<div class="dots">
<span
v-for="(_, index) in images"
:key="index"
:class="{ active: currentIndex === index }"
@click="currentIndex = index"
></span>
</div>
</div>
</template>
<script>
export default {
data() {
return {
images: ['image1.jpg', 'image2.jpg', 'image3.jpg'],
currentIndex: 0,
interval: null
}
},
mounted() {
this.startAutoPlay()
},
beforeDestroy() {
clearInterval(this.interval)
},
methods: {
startAutoPlay() {
this.interval = setInterval(() => {
this.currentIndex = (this.currentIndex + 1) % this.images.length
}, 3000)
}
}
}
</script>
<style scoped>
.carousel {
position: relative;
width: 100%;
height: 300px;
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;
}
.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 组件
安装 vue-awesome-swiper:
npm install vue-awesome-swiper swiper@5.4.5
实现代码:
<template>
<swiper :options="swiperOption" ref="mySwiper">
<swiper-slide v-for="(item, index) in images" :key="index">
<img :src="item" 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 {
images: ['image1.jpg', 'image2.jpg', 'image3.jpg'],
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 scoped>
.swiper-container {
height: 300px;
}
.swiper-slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
</style>






