当前位置:首页 > 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实现vue轮播

<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>

响应式断点设置

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

swiper实现vue轮播

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>

完整组件示例

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

<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 实现主题的方法 使用 CSS 变量动态切换主题 定义全局 CSS 变量,通过修改这些变量实现主题切换。在 Vue 的根组件或全局样式中定义变量: :root { --primary-co…

vue实现vnc

vue实现vnc

Vue 实现 VNC 客户端 在 Vue 项目中实现 VNC 客户端功能,可以通过集成现有的 VNC 客户端库或组件完成。以下是具体实现方法: 安装依赖库 使用 noVNC 或其他 VNC 客户端…

vue实现注销

vue实现注销

实现用户注销功能 在Vue应用中实现用户注销功能通常涉及清除用户会话、重置应用状态并跳转到登录页面。以下是具体实现方法: 清除用户认证信息 使用Vuex或Pinia管理用户状态时,需在注销时重置存储…

实现vue框架

实现vue框架

Vue框架的基本实现 Vue是一个渐进式JavaScript框架,用于构建用户界面。以下是实现Vue框架的基本步骤: 安装Vue 通过CDN引入Vue是最简单的方式: <script src…

vue 实现滑动

vue 实现滑动

Vue 实现滑动效果的方法 在Vue中实现滑动效果可以通过多种方式,以下是几种常见的方法: 使用CSS过渡和动画 通过Vue的<transition>组件结合CSS过渡或动画实现滑动效果…

vue实现pwa

vue实现pwa

Vue 实现 PWA 的步骤 Vue 结合 PWA(Progressive Web App)可以创建离线可用的 Web 应用。以下是实现方法: 安装 Vue CLI 并创建项目 确保已安装 Vue…