vue实现焦点轮播
Vue实现焦点轮播的方法
使用Vue的transition组件
在Vue中可以通过transition组件和CSS过渡效果实现轮播动画。定义多个轮播项,通过v-for循环渲染,利用v-show或v-if控制当前显示的项。
<template>
<div class="carousel">
<transition name="fade">
<div v-for="(item, index) in items" :key="index" v-show="currentIndex === index">
<img :src="item.image" :alt="item.title">
</div>
</transition>
<button @click="prev">上一张</button>
<button @click="next">下一张</button>
</div>
</template>
<script>
export default {
data() {
return {
currentIndex: 0,
items: [
{ image: 'image1.jpg', title: '图片1' },
{ image: 'image2.jpg', title: '图片2' },
{ image: 'image3.jpg', title: '图片3' }
]
}
},
methods: {
prev() {
this.currentIndex = (this.currentIndex - 1 + this.items.length) % this.items.length
},
next() {
this.currentIndex = (this.currentIndex + 1) % this.items.length
}
}
}
</script>
<style>
.fade-enter-active, .fade-leave-active {
transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {
opacity: 0;
}
</style>
使用第三方库vue-awesome-swiper
vue-awesome-swiper是基于Swiper的Vue组件,提供了丰富的轮播功能配置。
安装依赖:
npm install swiper vue-awesome-swiper --save
使用示例:
<template>
<swiper :options="swiperOption">
<swiper-slide v-for="(item, index) in items" :key="index">
<img :src="item.image" :alt="item.title">
</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 {
items: [
{ image: 'image1.jpg', title: '图片1' },
{ image: 'image2.jpg', title: '图片2' },
{ image: 'image3.jpg', title: '图片3' }
],
swiperOption: {
pagination: {
el: '.swiper-pagination',
clickable: true
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev'
},
loop: true,
autoplay: {
delay: 3000,
disableOnInteraction: false
}
}
}
}
}
</script>
自动轮播功能实现
通过setInterval实现自动轮播,注意在组件销毁时清除定时器。
export default {
data() {
return {
currentIndex: 0,
timer: null,
interval: 3000
}
},
mounted() {
this.startAutoPlay()
},
beforeDestroy() {
this.stopAutoPlay()
},
methods: {
startAutoPlay() {
this.timer = setInterval(() => {
this.next()
}, this.interval)
},
stopAutoPlay() {
if (this.timer) {
clearInterval(this.timer)
this.timer = null
}
}
}
}
响应式设计
可以通过监听窗口大小变化调整轮播图尺寸。
export default {
data() {
return {
windowWidth: 0
}
},
mounted() {
this.windowWidth = window.innerWidth
window.addEventListener('resize', this.handleResize)
},
beforeDestroy() {
window.removeEventListener('resize', this.handleResize)
},
methods: {
handleResize() {
this.windowWidth = window.innerWidth
}
}
}
指示器和小圆点
添加轮播图指示器,显示当前轮播位置。
<div class="indicators">
<span
v-for="(item, index) in items"
:key="index"
:class="{ active: currentIndex === index }"
@click="goTo(index)"
></span>
</div>
<style>
.indicators span {
display: inline-block;
width: 10px;
height: 10px;
margin: 0 5px;
border-radius: 50%;
background-color: #ccc;
cursor: pointer;
}
.indicators span.active {
background-color: #333;
}
</style>






