vue 幻灯片实现
Vue 幻灯片实现方法
使用第三方库(如 Swiper)
安装 Swiper 库:
npm install swiper
在 Vue 组件中引入并使用:
<template>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide" v-for="(slide, index) in slides" :key="index">
<img :src="slide.image" :alt="slide.title">
</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', title: 'Slide 1' },
{ image: 'image2.jpg', title: 'Slide 2' },
{ image: 'image3.jpg', title: 'Slide 3' }
]
}
},
mounted() {
new Swiper('.swiper-container', {
loop: true,
pagination: {
el: '.swiper-pagination',
clickable: true
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev'
}
})
}
}
</script>
自定义实现
通过 Vue 的响应式特性实现简单幻灯片:
<template>
<div class="slideshow">
<div class="slides-container">
<div
class="slide"
v-for="(slide, index) in slides"
:key="index"
:class="{ active: currentIndex === index }"
>
<img :src="slide.image" :alt="slide.title">
</div>
</div>
<button @click="prevSlide">Previous</button>
<button @click="nextSlide">Next</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' }
]
}
},
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
}
}
}
</script>
<style>
.slide {
display: none;
}
.slide.active {
display: block;
}
</style>
使用 Vue 动画过渡
添加过渡效果增强用户体验:
<template>
<div class="slideshow">
<transition name="fade" mode="out-in">
<div class="slide" :key="currentIndex">
<img :src="slides[currentIndex].image" :alt="slides[currentIndex].title">
</div>
</transition>
<button @click="prevSlide">Previous</button>
<button @click="nextSlide">Next</button>
</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' }
]
}
},
methods: {
nextSlide() {
this.currentIndex = (this.currentIndex + 1) % this.slides.length
},
prevSlide() {
this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length
}
}
}
</script>
<style>
.fade-enter-active, .fade-leave-active {
transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {
opacity: 0;
}
</style>
自动轮播功能
添加自动播放功能:
<script>
export default {
data() {
return {
currentIndex: 0,
slides: [...],
interval: null
}
},
mounted() {
this.startAutoPlay()
},
beforeDestroy() {
this.stopAutoPlay()
},
methods: {
startAutoPlay() {
this.interval = setInterval(() => {
this.nextSlide()
}, 3000)
},
stopAutoPlay() {
clearInterval(this.interval)
},
nextSlide() {...},
prevSlide() {...}
}
}
</script>
以上方法提供了从简单到复杂的 Vue 幻灯片实现方案,可根据项目需求选择合适的方式。第三方库如 Swiper 功能更全面,自定义实现则更灵活轻量。







