当前位置:首页 > VUE

swiper实现vue轮播

2026-02-17 00:45:25VUE

安装 Swiper 依赖

在 Vue 项目中安装 Swiper 核心库和 Vue 组件:

npm install swiper vue-awesome-swiper

引入 Swiper 组件

在需要使用的 Vue 文件中引入 Swiper 和配套样式:

import { Swiper, SwiperSlide } from 'swiper/vue'
import 'swiper/css'
import 'swiper/css/navigation'
import 'swiper/css/pagination'

基础轮播实现

在模板中使用 Swiper 组件,并绑定基础配置:

<template>
  <swiper
    :slides-per-view="3"
    :space-between="50"
    @swiper="onSwiper"
    @slideChange="onSlideChange"
  >
    <swiper-slide v-for="(item, index) in slides" :key="index">
      {{ item }}
    </swiper-slide>
  </swiper>
</template>

添加导航控制

引入导航模块并配置导航按钮:

import { Navigation } from 'swiper/modules'

export default {
  setup() {
    const onSwiper = (swiper) => {
      console.log(swiper)
    }
    const onSlideChange = () => {
      console.log('slide change')
    }
    return {
      modules: [Navigation],
      onSwiper,
      onSlideChange,
      slides: ['Slide 1', 'Slide 2', 'Slide 3']
    }
  }
}

添加分页指示器

引入分页模块并配置分页样式:

import { Pagination } from 'swiper/modules'

// 在组件配置中添加
modules: [Pagination],

模板中添加分页容器:

<swiper
  :modules="modules"
  :pagination="{ clickable: true }"
>
  <!-- slides -->
</swiper>

响应式配置

通过 breakpoints 参数实现响应式布局:

:breakpoints="{
  320: {
    slidesPerView: 1,
    spaceBetween: 20
  },
  768: {
    slidesPerView: 2,
    spaceBetween: 30
  },
  1024: {
    slidesPerView: 3,
    spaceBetween: 40
  }
}"

自动播放功能

引入自动播放模块并配置:

swiper实现vue轮播

import { Autoplay } from 'swiper/modules'

// 在组件配置中添加
modules: [Autoplay],
:autoplay="{
  delay: 2500,
  disableOnInteraction: false
}"

完整示例代码

<template>
  <swiper
    :modules="modules"
    :slides-per-view="3"
    :space-between="50"
    :pagination="{ clickable: true }"
    :autoplay="{ delay: 2500 }"
    :breakpoints="breakpoints"
    @swiper="onSwiper"
    @slideChange="onSlideChange"
  >
    <swiper-slide v-for="(item, index) in slides" :key="index">
      <div class="slide-content">{{ item }}</div>
    </swiper-slide>
  </swiper>
</template>

<script>
import { Swiper, SwiperSlide } from 'swiper/vue'
import { Navigation, Pagination, Autoplay } from 'swiper/modules'
import 'swiper/css'
import 'swiper/css/navigation'
import 'swiper/css/pagination'

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  setup() {
    const onSwiper = (swiper) => {
      console.log(swiper)
    }
    const onSlideChange = () => {
      console.log('slide change')
    }
    return {
      modules: [Navigation, Pagination, Autoplay],
      onSwiper,
      onSlideChange,
      slides: ['Slide 1', 'Slide 2', 'Slide 3', 'Slide 4', 'Slide 5'],
      breakpoints: {
        320: {
          slidesPerView: 1,
          spaceBetween: 20
        },
        768: {
          slidesPerView: 2,
          spaceBetween: 30
        },
        1024: {
          slidesPerView: 3,
          spaceBetween: 40
        }
      }
    }
  }
}
</script>

<style>
.slide-content {
  height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #eee;
  border-radius: 8px;
}
</style>

标签: swipervue
分享给朋友:

相关文章

vue前端实现下载进度

vue前端实现下载进度

实现下载进度条的基本思路 在Vue中实现下载进度条,通常需要结合XMLHttpRequest或Fetch API来监听下载进度事件。通过计算已下载数据与总数据的比例,动态更新进度条的显示。 使用XM…

vue实现alert

vue实现alert

实现自定义 Alert 组件 在 Vue 中可以通过组件化方式实现自定义 Alert 弹窗。以下是基于 Vue 3 的实现示例: 组件代码 (Alert.vue) <template>…

vue element实现

vue element实现

Vue Element UI 实现 Vue Element UI 是基于 Vue.js 的组件库,提供丰富的 UI 组件,适合快速开发企业级中后台产品。以下是一些关键实现方法和步骤: 安装 Vue…

vue实现文字

vue实现文字

Vue 中实现文字显示的方法 在 Vue 中实现文字显示可以通过多种方式,包括插值表达式、指令、组件等。以下是几种常见的实现方法: 插值表达式 使用双大括号 {{ }} 进行文本插值,这是 Vue…

vue实现海报

vue实现海报

Vue 实现海报生成方案 使用 html2canvas 库 html2canvas 是一个将 HTML 元素转换为 Canvas 的库,适合将 Vue 组件渲染为海报图片 安装依赖: npm in…

vue实现ide

vue实现ide

Vue 实现 IDE 的基本思路 使用 Vue 构建一个简易的集成开发环境(IDE)需要整合代码编辑器、文件管理、终端模拟等功能。以下是关键实现步骤和推荐工具。 核心功能模块 代码编辑器 使用…