本地实现vue轮播
使用vue-awesome-swiper实现轮播
安装vue-awesome-swiper库
npm install swiper vue-awesome-swiper --save
在组件中引入并使用
<template>
<swiper :options="swiperOption">
<swiper-slide v-for="(item, index) in slides" :key="index">
<img :src="item.image" alt="">
</swiper-slide>
<div class="swiper-pagination" slot="pagination"></div>
</swiper>
</template>
<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'
export default {
components: {
Swiper,
SwiperSlide
},
data() {
return {
slides: [
{ image: require('@/assets/slide1.jpg') },
{ image: require('@/assets/slide2.jpg') },
{ image: require('@/assets/slide3.jpg') }
],
swiperOption: {
pagination: {
el: '.swiper-pagination'
},
autoplay: {
delay: 3000
},
loop: true
}
}
}
}
</script>
<style scoped>
.swiper-container {
width: 100%;
height: 300px;
}
</style>
使用纯Vue实现简单轮播
创建基础轮播组件
<template>
<div class="carousel">
<div class="slides" :style="slideStyle">
<div
v-for="(slide, index) in slides"
:key="index"
class="slide"
>
<img :src="slide.image" alt="">
</div>
</div>
<button @click="prev">Prev</button>
<button @click="next">Next</button>
</div>
</template>
<script>
export default {
data() {
return {
currentIndex: 0,
slides: [
{ image: require('@/assets/slide1.jpg') },
{ image: require('@/assets/slide2.jpg') },
{ image: require('@/assets/slide3.jpg') }
]
}
},
computed: {
slideStyle() {
return {
transform: `translateX(-${this.currentIndex * 100}%)`
}
}
},
methods: {
prev() {
this.currentIndex =
this.currentIndex === 0
? this.slides.length - 1
: this.currentIndex - 1
},
next() {
this.currentIndex =
this.currentIndex === this.slides.length - 1
? 0
: this.currentIndex + 1
}
},
mounted() {
setInterval(this.next, 3000)
}
}
</script>
<style scoped>
.carousel {
position: relative;
width: 100%;
height: 300px;
overflow: hidden;
}
.slides {
display: flex;
width: 100%;
height: 100%;
transition: transform 0.5s ease;
}
.slide {
flex: 0 0 100%;
height: 100%;
}
.slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
</style>
添加过渡效果
在纯Vue实现基础上添加过渡动画
<template>
<div class="carousel">
<transition-group
name="slide"
tag="div"
class="slides"
>
<div
v-for="(slide, index) in slides"
v-show="index === currentIndex"
:key="index"
class="slide"
>
<img :src="slide.image" alt="">
</div>
</transition-group>
<button @click="prev">Prev</button>
<button @click="next">Next</button>
</div>
</template>
<style scoped>
.slide-enter-active,
.slide-leave-active {
transition: opacity 0.5s, transform 0.5s;
position: absolute;
width: 100%;
}
.slide-enter {
opacity: 0;
transform: translateX(100%);
}
.slide-leave-to {
opacity: 0;
transform: translateX(-100%);
}
</style>
响应式轮播实现
添加响应式断点处理
data() {
return {
windowWidth: window.innerWidth,
slidesToShow: 1
}
},
created() {
window.addEventListener('resize', this.handleResize)
this.handleResize()
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
methods: {
handleResize() {
this.windowWidth = window.innerWidth
if (this.windowWidth >= 1200) {
this.slidesToShow = 3
} else if (this.windowWidth >= 768) {
this.slidesToShow = 2
} else {
this.slidesToShow = 1
}
}
}
无限循环轮播优化
修改纯Vue实现中的计算方法
methods: {
prev() {
this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length
},
next() {
this.currentIndex = (this.currentIndex + 1) % this.slides.length
}
}






