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="">
</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/swiper-bundle.css';
export default {
data() {
return {
slides: [
{ image: 'image1.jpg' },
{ image: 'image2.jpg' },
{ image: 'image3.jpg' }
]
};
},
mounted() {
new Swiper('.swiper-container', {
loop: true,
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动画和Vue的数据绑定实现简单轮播:
<template>
<div class="carousel">
<div class="slides" :style="{ transform: `translateX(-${currentIndex * 100}%)` }">
<div class="slide" v-for="(item, index) in slides" :key="index">
<img :src="item.image" alt="">
</div>
</div>
<div class="dots">
<span
v-for="(item, index) in slides"
:key="index"
:class="{ active: currentIndex === index }"
@click="currentIndex = index"
></span>
</div>
</div>
</template>
<script>
export default {
data() {
return {
slides: [
{ image: 'image1.jpg' },
{ image: 'image2.jpg' },
{ image: 'image3.jpg' }
],
currentIndex: 0,
interval: null
};
},
mounted() {
this.interval = setInterval(() => {
this.currentIndex = (this.currentIndex + 1) % this.slides.length;
}, 3000);
},
beforeDestroy() {
clearInterval(this.interval);
}
};
</script>
<style scoped>
.carousel {
position: relative;
width: 100%;
height: 300px;
overflow: hidden;
}
.slides {
display: flex;
transition: transform 0.5s ease;
height: 100%;
}
.slide {
flex: 0 0 100%;
height: 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过渡动画实现轮播
利用Vue的过渡组件实现更丰富的动画效果:
<template>
<div class="carousel">
<transition :name="transitionName">
<div class="slide" :key="currentIndex">
<img :src="slides[currentIndex].image" alt="">
</div>
</transition>
<button @click="prev">Prev</button>
<button @click="next">Next</button>
</div>
</template>
<script>
export default {
data() {
return {
slides: [
{ image: 'image1.jpg' },
{ image: 'image2.jpg' },
{ image: 'image3.jpg' }
],
currentIndex: 0,
transitionName: 'slide-right'
};
},
methods: {
prev() {
this.transitionName = 'slide-right';
this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length;
},
next() {
this.transitionName = 'slide-left';
this.currentIndex = (this.currentIndex + 1) % this.slides.length;
}
}
};
</script>
<style scoped>
.carousel {
position: relative;
width: 100%;
height: 300px;
overflow: hidden;
}
.slide {
position: absolute;
width: 100%;
height: 100%;
}
.slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
.slide-right-enter-active,
.slide-right-leave-active,
.slide-left-enter-active,
.slide-left-leave-active {
transition: all 0.5s ease;
}
.slide-right-enter {
transform: translateX(-100%);
}
.slide-right-leave-to {
transform: translateX(100%);
}
.slide-left-enter {
transform: translateX(100%);
}
.slide-left-leave-to {
transform: translateX(-100%);
}
</style>
以上三种方法分别适合不同场景:Swiper库功能最全面,纯CSS实现轻量简单,Vue过渡动画则提供更多自定义动画效果。根据项目需求选择最适合的实现方式。







