当前位置:首页 > 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
  }
}"

自动播放功能

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

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>

swiper实现vue轮播

标签: swipervue
分享给朋友:

相关文章

vue实现适老化样式

vue实现适老化样式

Vue 实现适老化样式的关键方法 全局字体与字号调整 通过 CSS 变量或主题配置统一放大基础字号,建议正文不小于 18px,标题更大。在 App.vue 中设置全局样式: :root { --…

vue实现高亮

vue实现高亮

Vue 实现文本高亮的方法 在 Vue 中实现文本高亮可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-html 指令 通过 v-html 指令可以动态插入 HTML 内容,将需…

vue怎么实现

vue怎么实现

Vue 实现方法 Vue 是一个流行的前端框架,用于构建用户界面。以下是几种常见的实现方法: 数据绑定 使用 v-model 指令实现双向数据绑定,适用于表单输入元素。 <input v-m…

vue实现toast

vue实现toast

Vue 实现 Toast 的方法 使用第三方库(推荐) 对于快速实现 Toast 功能,推荐使用成熟的第三方库如 vue-toastification 或 vant 的 Toast 组件。 安装 v…

vue实现购票

vue实现购票

Vue 实现购票功能 数据结构设计 购票功能通常需要以下数据结构: 场次信息(时间、地点、价格) 座位信息(可选座位、已售座位) 用户订单信息 示例数据结构: data() { return…

vue实现树

vue实现树

Vue 实现树形结构的方法 使用递归组件实现树形结构 递归组件是Vue中实现树形结构的常见方式。通过组件调用自身,可以轻松构建多层嵌套的树形数据。 <template> <u…