当前位置:首页 > VUE

swiper实现vue轮播

2026-03-08 12:46:24VUE

安装 Swiper 依赖

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

npm install swiper vue-awesome-swiper

基础轮播实现

在 Vue 单文件组件中引入 Swiper:

<template>
  <swiper :options="swiperOptions">
    <swiper-slide v-for="(item, index) in slides" :key="index">
      <img :src="item.image" alt="">
    </swiper-slide>
    <div class="swiper-pagination" slot="pagination"></div>
  </swiper>
</template>

<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'

export default {
  components: { Swiper, SwiperSlide },
  data() {
    return {
      slides: [
        { image: 'image1.jpg' },
        { image: 'image2.jpg' }
      ],
      swiperOptions: {
        pagination: {
          el: '.swiper-pagination'
        },
        loop: true,
        autoplay: {
          delay: 3000
        }
      }
    }
  }
}
</script>

<style>
.swiper-container {
  height: 300px;
}
</style>

常用配置选项

通过修改 swiperOptions 实现不同效果:

swiperOptions: {
  // 分页器
  pagination: {
    el: '.swiper-pagination',
    clickable: true
  },

  // 导航按钮
  navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev'
  },

  // 自动播放
  autoplay: {
    delay: 2500,
    disableOnInteraction: false
  },

  // 无限循环
  loop: true,

  // 效果切换
  effect: 'fade', // 可选值: slide, fade, cube, coverflow, flip

  // 响应式配置
  breakpoints: {
    640: {
      slidesPerView: 1
    },
    768: {
      slidesPerView: 2
    }
  }
}

自定义样式

在组件中添加对应样式类名:

<template>
  <swiper class="custom-swiper">
    <!-- ... -->
    <div class="swiper-pagination custom-pagination" slot="pagination"></div>
    <div class="swiper-button-prev custom-prev" slot="button-prev"></div>
    <div class="swiper-button-next custom-next" slot="button-next"></div>
  </swiper>
</template>

<style>
.custom-swiper {
  --swiper-theme-color: #ff6600;
  --swiper-navigation-size: 30px;
}

.custom-pagination {
  bottom: 10px;
}

.custom-prev, .custom-next {
  background-color: rgba(0,0,0,0.5);
  padding: 15px;
  border-radius: 50%;
}
</style>

获取 Swiper 实例

通过 ref 获取 Swiper 实例进行控制:

<template>
  <swiper ref="mySwiper" @slideChange="onSlideChange">
    <!-- ... -->
  </swiper>
</template>

<script>
export default {
  methods: {
    onSlideChange() {
      const swiper = this.$refs.mySwiper.$swiper
      console.log('当前索引:', swiper.realIndex)
    },

    nextSlide() {
      this.$refs.mySwiper.$swiper.slideNext()
    }
  }
}
</script>

动态更新内容

当轮播数据变化时调用 update 方法:

swiper实现vue轮播

watch: {
  slides(newVal) {
    this.$nextTick(() => {
      this.$refs.mySwiper.$swiper.update()
    })
  }
}

标签: swipervue
分享给朋友:

相关文章

vue实现截图

vue实现截图

Vue 实现截图的方法 使用 html2canvas 库 安装 html2canvas 库: npm install html2canvas 在 Vue 组件中使用: import html2ca…

vue实现骰子

vue实现骰子

Vue 实现骰子功能 创建 Vue 组件 在 Vue 项目中创建一个骰子组件,例如 Dice.vue。该组件包含骰子的外观和逻辑。 <template> <div class=…

vue实现模块

vue实现模块

Vue 实现模块化的方法 Vue 支持多种模块化开发方式,可以根据项目需求选择适合的方案。 使用单文件组件(SFC) 单文件组件是 Vue 最常用的模块化方式,将模板、脚本和样式封装在一个 .vue…

vue实现网站

vue实现网站

Vue 实现网站的基本步骤 Vue.js 是一个渐进式 JavaScript 框架,适合构建现代化的单页应用(SPA)或动态网站。以下是使用 Vue 实现网站的基本方法。 初始化 Vue 项目 使用…

vue实现刷卡

vue实现刷卡

Vue 实现刷卡效果 在 Vue 中实现刷卡效果可以通过 CSS 动画和 Vue 的过渡系统结合完成。以下是一种常见的实现方式: 基础实现方法 安装 Vue 过渡依赖(如果尚未安装): npm i…

vue实现mvvm

vue实现mvvm

Vue 实现 MVVM 模式 MVVM(Model-View-ViewModel)是一种前端架构模式,Vue 是其典型实现之一。MVVM 的核心是通过数据绑定和响应式系统实现视图与数据的自动同步。…