vue实现轮播滚动显示
使用Swiper插件实现轮播
Swiper是一个流行的开源触摸滑动插件,专为移动设备优化,同时兼容PC端。在Vue项目中可以通过swiper/vue包快速集成。
安装依赖:
npm install swiper vue-awesome-swiper
基础实现代码:
<template>
<swiper
:modules="modules"
:slides-per-view="3"
:space-between="50"
:autoplay="{ delay: 2500 }"
@swiper="onSwiper"
>
<swiper-slide v-for="(slide, index) in slides" :key="index">
<img :src="slide.image" />
</swiper-slide>
</swiper>
</template>
<script>
import { Autoplay } from 'swiper'
import { Swiper, SwiperSlide } from 'swiper/vue'
export default {
components: { Swiper, SwiperSlide },
setup() {
return {
modules: [Autoplay],
slides: [
{ image: 'image1.jpg' },
{ image: 'image2.jpg' },
{ image: 'image3.jpg' }
]
}
}
}
</script>
纯CSS实现轮播效果
通过CSS动画和overflow-hidden容器可以实现基础轮播效果,适合轻量级需求。
<template>
<div class="carousel-container">
<div class="carousel-track" :style="trackStyle">
<div v-for="(item, index) in items" :key="index" class="slide">
{{ item.content }}
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
currentIndex: 0,
items: [
{ content: 'Slide 1' },
{ content: 'Slide 2' },
{ content: 'Slide 3' }
]
}
},
computed: {
trackStyle() {
return {
transform: `translateX(-${this.currentIndex * 100}%)`,
transition: 'transform 0.5s ease'
}
}
},
mounted() {
setInterval(() => {
this.currentIndex = (this.currentIndex + 1) % this.items.length
}, 3000)
}
}
</script>
<style>
.carousel-container {
width: 100%;
overflow: hidden;
}
.carousel-track {
display: flex;
width: 300%;
}
.slide {
flex: 1;
min-width: 33.33%;
}
</style>
使用Vue Transition组件
结合Vue的过渡系统实现更丰富的动画效果。
<template>
<div class="carousel">
<transition-group name="slide" tag="div">
<div v-for="(item, index) in visibleItems" :key="item.id" class="item">
{{ item.text }}
</div>
</transition-group>
<button @click="next">Next</button>
</div>
</template>
<script>
export default {
data() {
return {
items: [
{ id: 1, text: 'Item 1' },
{ id: 2, text: 'Item 2' },
{ id: 3, text: 'Item 3' }
],
currentIndex: 0
}
},
computed: {
visibleItems() {
return [this.items[this.currentIndex]]
}
},
methods: {
next() {
this.currentIndex = (this.currentIndex + 1) % this.items.length
}
},
mounted() {
setInterval(this.next, 2000)
}
}
</script>
<style>
.slide-enter-active, .slide-leave-active {
transition: all 0.5s ease;
}
.slide-enter-from {
transform: translateX(100%);
}
.slide-leave-to {
transform: translateX(-100%);
}
</style>
响应式轮播实现
根据不同屏幕尺寸调整显示数量,需要结合ResizeObserver API。
<template>
<div ref="container" class="carousel">
<div class="slides" :style="slidesStyle">
<div v-for="(item, index) in items" :key="index" class="slide">
{{ item }}
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
items: ['Slide 1', 'Slide 2', 'Slide 3', 'Slide 4'],
visibleCount: 1,
containerWidth: 0
}
},
mounted() {
this.updateVisibleCount()
const observer = new ResizeObserver(this.updateVisibleCount)
observer.observe(this.$refs.container)
},
methods: {
updateVisibleCount() {
const width = this.$refs.container.clientWidth
this.visibleCount = width > 768 ? 3 : width > 480 ? 2 : 1
this.containerWidth = width
}
},
computed: {
slidesStyle() {
return {
width: `${(this.items.length / this.visibleCount) * 100}%`,
transform: `translateX(-${this.currentIndex * (100 / this.items.length)}%)`
}
}
}
}
</script>






