vue实现顶部轮播图
使用Swiper实现轮播图
安装Swiper库
npm install swiper@6
引入Swiper样式和组件
<template>
<div class="banner-container">
<swiper ref="mySwiper" :options="swiperOptions">
<swiper-slide v-for="(item, index) in banners" :key="index">
<img :src="item.imageUrl" class="banner-image" />
</swiper-slide>
<div class="swiper-pagination" slot="pagination"></div>
</swiper>
</div>
</template>
<script>
import { Swiper, SwiperSlide } from 'swiper/vue'
import 'swiper/swiper-bundle.min.css'
import SwiperCore, { Pagination, Autoplay } from 'swiper'
SwiperCore.use([Pagination, Autoplay])
export default {
components: {
Swiper,
SwiperSlide
},
data() {
return {
banners: [
{ imageUrl: 'https://example.com/banner1.jpg' },
{ imageUrl: 'https://example.com/banner2.jpg' },
{ imageUrl: 'https://example.com/banner3.jpg' }
],
swiperOptions: {
pagination: {
el: '.swiper-pagination',
clickable: true
},
autoplay: {
delay: 3000,
disableOnInteraction: false
},
loop: true
}
}
}
}
</script>
<style scoped>
.banner-container {
width: 100%;
height: 300px;
}
.banner-image {
width: 100%;
height: 100%;
object-fit: cover;
}
.swiper-pagination-bullet-active {
background: #fff;
}
</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="'Slide ' + index">
</div>
</div>
<button class="prev" @click="prev">❮</button>
<button class="next" @click="next">❯</button>
</div>
</template>
<script>
export default {
props: {
slides: {
type: Array,
required: true,
default: () => [
{ image: 'https://example.com/slide1.jpg' },
{ image: 'https://example.com/slide2.jpg' },
{ image: 'https://example.com/slide3.jpg' }
]
},
interval: {
type: Number,
default: 3000
}
},
data() {
return {
currentIndex: 0,
timer: null
}
},
computed: {
slideStyle() {
return {
transform: `translateX(-${this.currentIndex * 100}%)`
}
}
},
mounted() {
this.startAutoPlay()
},
beforeDestroy() {
this.stopAutoPlay()
},
methods: {
startAutoPlay() {
this.timer = setInterval(this.next, this.interval)
},
stopAutoPlay() {
clearInterval(this.timer)
},
next() {
this.currentIndex = (this.currentIndex + 1) % this.slides.length
},
prev() {
this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length
}
}
}
</script>
<style>
.carousel {
position: relative;
width: 100%;
height: 300px;
overflow: hidden;
}
.slides {
display: flex;
height: 100%;
transition: transform 0.5s ease;
}
.slide {
min-width: 100%;
height: 100%;
}
.slide img {
width: 100%;
height: 100%;
object-fit: cover;
}
.prev, .next {
position: absolute;
top: 50%;
transform: translateY(-50%);
background: rgba(0,0,0,0.5);
color: white;
border: none;
padding: 10px;
cursor: pointer;
z-index: 10;
}
.prev {
left: 10px;
}
.next {
right: 10px;
}
</style>
响应式设计要点
设置不同断点的样式
@media (max-width: 768px) {
.banner-container {
height: 200px;
}
}
@media (max-width: 480px) {
.banner-container {
height: 150px;
}
}
性能优化建议
图片懒加载实现
<template>
<img
v-for="(item, index) in banners"
:key="index"
:src="currentIndex === index ? item.imageUrl : ''"
:alt="'Banner ' + index"
class="banner-image"
/>
</template>
预加载重要图片
mounted() {
this.banners.forEach(banner => {
const img = new Image()
img.src = banner.imageUrl
})
}






