当前位置:首页 > VUE

swiper实现vue轮播

2026-03-28 23:40:49VUE

安装 Swiper 依赖

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

npm install swiper vue-awesome-swiper

引入 Swiper 组件

在需要使用的 Vue 文件中引入 Swiper 和 SwiperSlide:

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

基础轮播实现

模板部分添加 Swiper 容器和幻灯片:

<template>
  <swiper>
    <swiper-slide>Slide 1</swiper-slide>
    <swiper-slide>Slide 2</swiper-slide>
    <swiper-slide>Slide 3</swiper-slide>
  </swiper>
</template>

添加导航控件

引入导航模块并配置:

import { Navigation } from 'swiper/modules';
// 在 setup 中
const modules = [Navigation];

模板中添加导航元素:

<swiper :modules="modules" navigation>
  <!-- slides -->
  <template #container-end>
    <div class="swiper-button-next"></div>
    <div class="swiper-button-prev"></div>
  </template>
</swiper>

自动轮播配置

通过参数控制自动播放:

const autoplayOptions = {
  delay: 2500,
  disableOnInteraction: false
};

模板中使用:

<swiper :autoplay="autoplayOptions">
  <!-- slides -->
</swiper>

响应式断点设置

配置不同屏幕尺寸的参数:

const breakpoints = {
  640: { slidesPerView: 2 },
  768: { slidesPerView: 3 },
  1024: { slidesPerView: 4 }
};

模板中应用:

<swiper :breakpoints="breakpoints">
  <!-- slides -->
</swiper>

自定义样式

覆盖默认样式需使用深度选择器:

<style scoped>
:deep(.swiper-pagination-bullet-active) {
  background-color: #ff0000;
}
</style>

完整组件示例

组合所有功能的完整组件:

swiper实现vue轮播

<template>
  <swiper
    :modules="modules"
    :slides-per-view="1"
    :space-between="50"
    :pagination="{ clickable: true }"
    :autoplay="autoplayOptions"
    :breakpoints="breakpoints"
  >
    <swiper-slide v-for="(slide, index) in slides" :key="index">
      {{ slide }}
    </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() {
    return {
      modules: [Navigation, Pagination, Autoplay],
      slides: ['Content 1', 'Content 2', 'Content 3'],
      autoplayOptions: { delay: 3000 },
      breakpoints: {
        640: { slidesPerView: 2 },
        1024: { slidesPerView: 3 }
      }
    };
  }
};
</script>

标签: swipervue
分享给朋友:

相关文章

vue实现倒计时抢券

vue实现倒计时抢券

Vue 实现倒计时抢券功能 核心逻辑 倒计时抢券功能需要结合时间计算、状态管理和界面交互。通过 Vue 的响应式特性和生命周期钩子,可以高效实现这一需求。 实现步骤 1. 数据准备 在 Vue 组件…

vue实现list

vue实现list

Vue 实现列表渲染 在 Vue 中,可以使用 v-for 指令来渲染列表。v-for 指令基于一个数组或对象来渲染一个列表,可以遍历数组或对象的属性。 基本列表渲染 <template>…

vue实现加减

vue实现加减

Vue 实现加减功能 在 Vue 中实现加减功能可以通过数据绑定和事件监听来完成。以下是一个简单的实现方法: 模板部分 <template> <div> <…

vue 实现长按

vue 实现长按

实现长按功能的基本思路 在Vue中实现长按功能,通常需要监听元素的mousedown或touchstart事件,启动一个计时器。当用户按住元素超过设定的时间阈值时,触发长按回调函数。如果在时间阈值内触…

vue实现导出excel实现流程

vue实现导出excel实现流程

安装依赖库 需要安装 xlsx 和 file-saver 库来处理 Excel 文件的生成和保存。通过 npm 或 yarn 安装: npm install xlsx file-saver # 或…

vue怎么实现 tab

vue怎么实现 tab

Vue 实现 Tab 的方法 使用动态组件和 v-if 通过动态组件或 v-if 指令切换不同 Tab 内容,结合点击事件改变当前激活的 Tab。 <template> &l…