swiper实现vue轮播
使用 Swiper 实现 Vue 轮播
Swiper 是一个流行的开源滑动组件,支持 Vue、React 等框架。以下是基于 Vue 3 的实现方法。
安装 Swiper 依赖
需要安装 Swiper 核心库和 Vue 组件:

npm install swiper vue-awesome-swiper
基础轮播实现
在 Vue 组件中引入 Swiper 并配置:

<template>
<swiper
:modules="modules"
:slides-per-view="1"
:space-between="50"
navigation
pagination
@swiper="onSwiper"
>
<swiper-slide v-for="(slide, index) in slides" :key="index">
<img :src="slide.image" :alt="slide.title">
</swiper-slide>
</swiper>
</template>
<script>
import { Swiper, SwiperSlide } from 'swiper/vue'
import { Navigation, Pagination } from 'swiper/modules'
import 'swiper/css'
import 'swiper/css/navigation'
import 'swiper/css/pagination'
export default {
components: { Swiper, SwiperSlide },
setup() {
const slides = [
{ image: 'image1.jpg', title: 'Slide 1' },
{ image: 'image2.jpg', title: 'Slide 2' }
]
return {
modules: [Navigation, Pagination],
slides
}
}
}
</script>
常用配置参数
slidesPerView: 同时显示的幻灯片数量spaceBetween: 幻灯片间距(像素)loop: 启用循环模式autoplay: 自动播放配置breakpoints: 响应式断点配置
自动播放示例
添加自动播放功能需导入 Autoplay 模块:
import { Autoplay } from 'swiper/modules'
// 在 setup 中
modules: [Autoplay],
autoplay: {
delay: 2500,
disableOnInteraction: false
}
响应式设计
通过 breakpoints 实现不同屏幕尺寸的适配:
breakpoints: {
640: {
slidesPerView: 2
},
1024: {
slidesPerView: 3
}
}
注意事项
- 确保正确导入所需的 CSS 文件
- 导航按钮和分页器需要额外样式导入
- 在 Nuxt.js 中使用时需要额外配置 SSR
- 动态内容更新可能需要调用
swiper.update()方法
以上实现方式适用于大多数基础轮播场景,Swiper 还支持垂直滑动、3D 效果、缩略图等高级功能。






