vue实现div轮播
Vue实现div轮播的方法
使用vue-awesome-swiper插件
安装vue-awesome-swiper插件:
npm install swiper vue-awesome-swiper --save
在组件中引入并使用:
import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'
export default {
components: {
Swiper,
SwiperSlide
},
data() {
return {
swiperOptions: {
autoplay: {
delay: 3000,
disableOnInteraction: false
},
loop: true,
pagination: {
el: '.swiper-pagination',
clickable: true
}
},
slides: [
{ id: 1, content: 'Slide 1' },
{ id: 2, content: 'Slide 2' },
{ id: 3, content: 'Slide 3' }
]
}
}
}
模板部分:
<swiper :options="swiperOptions">
<swiper-slide v-for="slide in slides" :key="slide.id">
<div class="slide-content">{{ slide.content }}</div>
</swiper-slide>
<div class="swiper-pagination" slot="pagination"></div>
</swiper>
纯Vue实现轮播
创建基础轮播组件:
export default {
data() {
return {
currentIndex: 0,
slides: [
{ id: 1, content: 'Slide 1' },
{ id: 2, content: 'Slide 2' },
{ id: 3, content: 'Slide 3' }
],
timer: null
}
},
mounted() {
this.startAutoPlay()
},
beforeDestroy() {
clearInterval(this.timer)
},
methods: {
startAutoPlay() {
this.timer = setInterval(() => {
this.nextSlide()
}, 3000)
},
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
}
}
}
模板部分:
<div class="carousel-container">
<div class="carousel-slides" :style="{ transform: `translateX(-${currentIndex * 100}%)` }">
<div class="slide" v-for="(slide, index) in slides" :key="slide.id">
{{ slide.content }}
</div>
</div>
<button class="prev-btn" @click="prevSlide">Prev</button>
<button class="next-btn" @click="nextSlide">Next</button>
<div class="indicators">
<span
v-for="(slide, index) in slides"
:key="index"
@click="goToSlide(index)"
:class="{ active: currentIndex === index }"
></span>
</div>
</div>
CSS样式:
.carousel-container {
position: relative;
overflow: hidden;
width: 100%;
height: 300px;
}
.carousel-slides {
display: flex;
transition: transform 0.5s ease;
height: 100%;
}
.slide {
flex: 0 0 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
font-size: 2rem;
}
.prev-btn, .next-btn {
position: absolute;
top: 50%;
transform: translateY(-50%);
z-index: 10;
}
.prev-btn {
left: 10px;
}
.next-btn {
right: 10px;
}
.indicators {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
}
.indicators span {
width: 12px;
height: 12px;
margin: 0 5px;
border-radius: 50%;
background: #ccc;
cursor: pointer;
}
.indicators span.active {
background: #333;
}
使用Vue Transition组件实现动画效果
在纯Vue实现的基础上添加过渡效果:
<div class="carousel-container">
<transition-group
name="slide"
tag="div"
class="carousel-slides"
>
<div
class="slide"
v-for="(slide, index) in slides"
:key="slide.id"
v-show="currentIndex === index"
>
{{ slide.content }}
</div>
</transition-group>
</div>
添加过渡CSS:
.slide-enter-active, .slide-leave-active {
transition: opacity 0.5s ease;
position: absolute;
width: 100%;
}
.slide-enter, .slide-leave-to {
opacity: 0;
}






