vue实现图片轮播
使用 Swiper 实现图片轮播
安装 Swiper 依赖
npm install swiper
在 Vue 组件中引入 Swiper
<template>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide" v-for="(image, index) in images" :key="index">
<img :src="image" alt="轮播图">
</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 {
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>
使用原生 Vue 实现简单轮播
基础实现方案
<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>
<button @click="prev">上一张</button>
<button @click="next">下一张</button>
</div>
</template>
<script>
export default {
data() {
return {
images: [
'image1.jpg',
'image2.jpg',
'image3.jpg'
],
currentIndex: 0,
timer: null
}
},
methods: {
prev() {
this.currentIndex = (this.currentIndex - 1 + this.images.length) % this.images.length
},
next() {
this.currentIndex = (this.currentIndex + 1) % this.images.length
},
startAutoPlay() {
this.timer = setInterval(this.next, 3000)
},
stopAutoPlay() {
clearInterval(this.timer)
}
},
mounted() {
this.startAutoPlay()
},
beforeDestroy() {
this.stopAutoPlay()
}
}
</script>
<style scoped>
.carousel {
position: relative;
width: 100%;
height: 300px;
overflow: hidden;
}
.slides {
display: flex;
transition: transform 0.5s ease;
}
.slide {
flex: 0 0 100%;
}
.slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
</style>
使用第三方组件 vue-awesome-swiper
安装组件
npm install vue-awesome-swiper swiper@5.4.5
实现代码示例
<template>
<swiper :options="swiperOption">
<swiper-slide v-for="(image, index) in images" :key="index">
<img :src="image" 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 {
width: 100%;
height: 300px;
}
.swiper-slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
</style>






