🎄🎅 节日促销:高达 50% 折扣 UI Initiative, Swiper Studio t0ggles 🎅🎄

Swiper Vue.js 组件

Swiper Vue 组件很可能会在未来的版本中被移除。建议迁移到 Swiper Element
如果您是从 Swiper 9 升级到 Swiper 10,请查看 Swiper 10 迁移指南
如果您正在查找 v9 文档,请访问这里 v9.swiperjs.com

安装

Swiper Vue.js 插件仅可通过 NPM 作为主要 Swiper 库的一部分提供

  npm i swiper

用法

swiper/vue 导出 2 个组件:SwiperSwiperSlide

<template>
  <swiper
    :slides-per-view="3"
    :space-between="50"
    @swiper="onSwiper"
    @slideChange="onSlideChange"
  >
    <swiper-slide>Slide 1</swiper-slide>
    <swiper-slide>Slide 2</swiper-slide>
    <swiper-slide>Slide 3</swiper-slide>
    ...
  </swiper>
</template>
<script>
  // Import Swiper Vue.js components
  import { Swiper, SwiperSlide } from 'swiper/vue';

  // Import Swiper styles
  import 'swiper/css';

  export default {
    components: {
      Swiper,
      SwiperSlide,
    },
    setup() {
      const onSwiper = (swiper) => {
        console.log(swiper);
      };
      const onSlideChange = () => {
        console.log('slide change');
      };
      return {
        onSwiper,
        onSlideChange,
      };
    },
  };
</script>
默认情况下,Swiper Vue 使用 Swiper 的核心版本(没有任何其他模块)。如果您想使用 Navigation、Pagination 和 其他模块,您必须先安装它们

这是从 swiper/modules 导入的附加模块的列表

  • Virtual - 虚拟幻灯片模块
  • Keyboard - 键盘控制模块
  • Mousewheel - 鼠标滚轮控制模块
  • Navigation - 导航模块
  • Pagination - 分页模块
  • Scrollbar - 滚动条模块
  • Parallax - 视差模块
  • FreeMode - 自由模式模块
  • Grid - 网格模块
  • Manipulation - 幻灯片操作模块(仅适用于核心版本)
  • Zoom - 缩放模块
  • Controller - 控制器模块
  • A11y - 无障碍模块
  • History - 历史导航模块
  • HashNavigation - 哈希导航模块
  • Autoplay - 自动播放模块
  • EffectFade - 淡入淡出效果模块
  • EffectCube - 立方体效果模块
  • EffectFlip - 翻转效果模块
  • EffectCoverflow - Coverflow 效果模块
  • EffectCards - 卡片效果模块
  • EffectCreative - 创意效果模块
  • Thumbs - 缩略图模块
<template>
  <swiper
    :modules="modules"
    :slides-per-view="3"
    :space-between="50"
    navigation
    :pagination="{ clickable: true }"
    :scrollbar="{ draggable: true }"
    @swiper="onSwiper"
    @slideChange="onSlideChange"
  >
    <swiper-slide>Slide 1</swiper-slide>
    <swiper-slide>Slide 2</swiper-slide>
    <swiper-slide>Slide 3</swiper-slide>
    ...
  </swiper>
</template>
<script>
  // import Swiper core and required modules
  import { Navigation, Pagination, Scrollbar, A11y } from 'swiper/modules';

  // Import Swiper Vue.js components
  import { Swiper, SwiperSlide } from 'swiper/vue';

  // Import Swiper styles
  import 'swiper/css';
  import 'swiper/css/navigation';
  import 'swiper/css/pagination';
  import 'swiper/css/scrollbar';

  // Import Swiper styles
  export default {
    components: {
      Swiper,
      SwiperSlide,
    },
    setup() {
      const onSwiper = (swiper) => {
        console.log(swiper);
      };
      const onSlideChange = () => {
        console.log('slide change');
      };
      return {
        onSwiper,
        onSlideChange,
        modules: [Navigation, Pagination, Scrollbar, A11y],
      };
    },
  };
</script>
注意,如果您在没有指定其元素的情况下传递这些参数(例如,没有 navigation.nextElpagination.el 等),Swiper Vue 组件将为 Navigation、Pagination 和 Scrollbar 创建所需的元素

样式

Swiper 包包含不同的 CSS、Less 和 SCSS 样式集

  • swiper/css - 仅核心 Swiper 样式
  • swiper/css/bundle - 所有 Swiper 样式,包括所有模块样式(如 Navigation、Pagination 等)

模块样式(如果您已经导入了捆绑样式,则不是必需的)

  • swiper/css/a11y - A11y 模块所需的样式
  • swiper/css/autoplay - Autoplay 模块所需的样式
  • swiper/css/controller - Controller 模块所需的样式
  • swiper/css/effect-cards - Cards 效果模块所需的样式
  • swiper/css/effect-coverflow - Coverflow 效果模块所需的样式
  • swiper/css/effect-creative - Creative 效果模块所需的样式
  • swiper/css/effect-cube - Cube 效果模块所需的样式
  • swiper/css/effect-fade - Fade 效果模块所需的样式
  • swiper/css/effect-flip - Flip 效果模块所需的样式
  • swiper/css/free-mode - Free Mode 模块所需的样式
  • swiper/css/grid - Grid 模块所需的样式
  • swiper/css/hash-navigation - Hash Navigation 模块所需的样式
  • swiper/css/history - History 模块所需的样式
  • swiper/css/keyboard - Keyboard 模块所需的样式
  • swiper/css/manipulation - Manipulation 模块所需的样式
  • swiper/css/mousewheel - Mousewheel 模块所需的样式
  • swiper/css/navigation - Navigation 模块所需的样式
  • swiper/css/pagination - Pagination 模块所需的样式
  • swiper/css/parallax - Parallax 模块所需的样式
  • swiper/css/scrollbar - Scrollbar 模块所需的样式
  • swiper/css/thumbs - Thumbs 模块所需的样式
  • swiper/css/virtual - Virtual 模块所需的样式
  • swiper/css/zoom - Zoom 模块所需的样式

对于 Less 样式,请在导入路径中将 css 替换为 less,例如

import 'swiper/less';
import 'swiper/less/navigation';
import 'swiper/less/pagination';

对于 SCSS 样式,请在导入路径中将 css 替换为 scss,例如

import 'swiper/scss';
import 'swiper/scss/navigation';
import 'swiper/scss/pagination';

Swiper props

Swiper Vue.js 组件接收所有 Swiper 参数 作为组件 props,以及一些额外的 props

Prop类型默认值描述
tagstring'div'主 Swiper 容器 HTML 元素标签
wrapperTagstring'div'Swiper 包裹器 HTML 元素标签

Swiper 事件

Swiper 组件支持所有 Swiper 事件,包括额外的 swiper 事件,该事件会尽快返回 swiper 实例。例如

<swiper @swiper="..." @slideChange="..." @reachEnd="..." ...></swiper>

SwiperSlide props

Prop类型默认值描述
tagstring'div'Swiper Slide HTML 元素标签
zoombooleanfalse启用缩放模式所需的额外包裹器
virtualIndexnumber实际 swiper 幻灯片索引。必须为虚拟幻灯片设置

SwiperSlide 插槽 props

SwiperSlide 组件具有以下插槽 props

  • isActive - 当当前幻灯片处于活动状态时为 true
  • isPrev - 当当前幻灯片是活动幻灯片的前一个幻灯片时为 true
  • isNext - 当当前幻灯片是活动幻灯片的下一个幻灯片时为 true
  • isVisible - 当当前幻灯片可见时为 true(必须启用 watchSlidesProgress Swiper 参数)
  • isDuplicate - 当当前幻灯片是重复幻灯片时为 true(启用 loop 模式时)。例如
<swiper>
  <swiper-slide v-slot="{ isActive }">
    <div>Current slide is {{ isActive ? 'active' : 'not active' }}</div>
  </swiper-slide>
</swiper>

useSwiper

Swiper Vue 提供 useSwiper hook 以便在 Swiper 内的组件中轻松获取 Swiper 实例

<!-- some-inner-component.vue -->
<template>
  <button @click="swiper.slideNext()">Slide to the next slide</button>
</template>
<script>
  import { useSwiper } from 'swiper/vue';

  export default {
    setup() {
      const swiper = useSwiper();

      return {
        swiper,
      };
    },
  };
</script>

useSwiperSlide

useSwiperSlide 是另一个用于 Swiper 幻灯片内部的组件的 hook,用于获取幻灯片数据(与 SwiperSlide 插槽 props 中的数据相同)

<!-- some-inner-component.vue -->
<template>
  <p>Current slide is {{swiperSlide.isActive ? 'active' : 'not active'}}</p>
</template>
<script>
  import { useSwiperSlide } from 'swiper/vue';

  export default {
    setup() {
      const swiperSlide = useSwiperSlide();

      return {
        swiperSlide,
      };
    },
  };
</script>

插槽

Swiper Vue.js 组件使用“插槽”进行内容分发。有 4 个可用插槽

  • container-start - 元素将添加到 swiper-container 的开头
  • container-end (默认) - 元素将添加到 swiper-container 的末尾
  • wrapper-start - 元素将添加到 swiper-wrapper 的开头
  • wrapper-end - 元素将添加到 swiper-wrapper 的末尾

例如

<swiper>
  <swiper-slide>Slide 1</swiper-slide>
  <swiper-slide>Slide 2</swiper-slide>
  <template v-slot:container-start><span>Container start</span></template>
  <template v-slot:container-end><span>Container end</span></template>
  <template v-slot:wrapper-start><span>Wrapper start</span></template>
  <template v-slot:wrapper-end><span>Wrapper end</span></template>
</swiper>

将呈现为

<div class="swiper">
  <span>Container start</span>
  <div class="swiper-wrapper">
    <span>Wrapper start</span>
    <div class="swiper-slide">Slide 1</div>
    <div class="swiper-slide">Slide 2</div>
    <span>Wrapper end</span>
  </div>
  <span>Container end</span>
</div>

虚拟幻灯片

此处的虚拟幻灯片渲染完全由 Vue.js 处理,不需要任何操作,只需设置 :virtual="true" 属性并在幻灯片上设置 virtualIndex

<template>
  <swiper :modules="[Virtual]" :slides-per-view="3" :space-between="50" virtual>
    <swiper-slide
      v-for="(slideContent, index) in slides"
      :key="index"
      :virtualIndex="index"
      >{{slideContent}}</swiper-slide
    >
  </swiper>
</template>
<script>
  import { Virtual } from 'swiper/modules';
  import { Swiper, SwiperSlide } from 'swiper/vue';

  export default {
    components: {
      Swiper,
      SwiperSlide,
    },

    setup() {
      // Create array with 1000 slides
      const slides = Array.from({ length: 1000 }).map(
        (el, index) => `Slide ${index + 1}`
      );
      return {
        slides,
        Virtual,
      };
    },
  };
</script>

控制器

控制器需要将一个 Swiper 实例传递给另一个

<template>
  <!-- Main Swiper -> pass controlled swiper instance -->
  <swiper
    :modules="[Controller]"
    :controller="{ control: controlledSwiper }"
    ...
  >
    ...
  </swiper>

  <!-- Controlled Swiper -> store swiper instance -->
  <swiper :modules="[Controller]" @swiper="setControlledSwiper" ...>
    ...
  </swiper>
</template>
<script>
  import { ref } from 'vue';
  import { Controller } from 'swiper/modules';
  import { Swiper, SwiperSlide } from 'swiper/vue';

  export default {
    components: {
      Swiper,
      SwiperSlide,
    },
    setup() {
      const controlledSwiper = ref(null);
      const setControlledSwiper = (swiper) => {
        controlledSwiper.value = swiper;
      };
      return {
        Controller,
        controlledSwiper,
        setControlledSwiper,
      };
    },
  };
</script>

对于双向控制(当两个 Swiper 相互控制时),它应该是这样的

<template>
  <swiper
    :modules="[Controller]"
    @swiper="setFirstSwiper"
    :controller="{ control: secondSwiper }"
    ...
  >
    ...
  </swiper>
  <swiper
    :modules="[Controller]"
    @swiper="setSecondSwiper"
    :controller="{ control: firstSwiper }"
    ...
  >
    ...
  </swiper>
</template>
<script>
  import { ref } from 'vue';
  import { Controller } from 'swiper/modules';
  import { Swiper, SwiperSlide } from 'swiper/vue';

  export default {
    components: {
      Swiper,
      SwiperSlide,
    },
    setup() {
      const firstSwiper = ref(null);
      const secondSwiper = ref(null);
      const setFirstSwiper = (swiper) => {
        firstSwiper.value = swiper;
      };
      const setSecondSwiper = (swiper) => {
        secondSwiper.value = swiper;
      };

      return {
        Controller,
        firstSwiper,
        secondSwiper,
        setFirstSwiper,
        setSecondSwiper,
      };
    },
  };
</script>

缩略图

与控制器相同,我们需要存储缩略图实例并将其传递给主图库

<template>
  <!-- Main Swiper -> pass thumbs swiper instance -->
  <swiper :modules="[Thumbs]" :thumbs="{ swiper: thumbsSwiper }" ...>
    ...
  </swiper>

  <!-- Thumbs Swiper -> store swiper instance -->
  <!-- It is also required to set watchSlidesProgress prop -->
  <swiper
    :modules="[Thumbs]"
    watch-slides-progress
    @swiper="setThumbsSwiper"
    ...
  >
    ...
  </swiper>
</template>
<script>
  import { ref } from 'vue';
  import { Thumbs } from 'swiper/modules';
  import { Swiper, SwiperSlide } from 'swiper/vue';

  export default {
    components: {
      Swiper,
      SwiperSlide,
    },
    setup() {
      const thumbsSwiper = ref(null);
      const setThumbsSwiper = (swiper) => {
        thumbsSwiper.value = swiper;
      };
      return {
        Thumbs,
        thumbsSwiper,
        setThumbsSwiper,
      };
    },
  };
</script>

效果

以下效果可用

  • 淡入淡出
  • 立方体
  • Coverflow
  • 翻转
  • 卡片
  • 创意

要使用效果,您必须先导入并安装它们(与其他所有模块一样)。

您可以在这里找到运行中的 效果演示

完整的淡入淡出效果示例

<template>
  <swiper :modules="[EffectFade]" effect="fade">
    <swiper-slide>Slide 1</swiper-slide>
    <swiper-slide>Slide 2</swiper-slide>
    <swiper-slide>Slide 3</swiper-slide>
    ...
  </swiper>
</template>
<script>
  import { EffectFade } from 'swiper/modules';
  import { Swiper, SwiperSlide } from 'swiper/vue';

  import 'swiper/css';
  import 'swiper/css/effect-fade';

  export default {
    components: {
      Swiper,
      SwiperSlide,
    },
    setup() {
      return {
        EffectFade,
      };
    },
  };
</script>

接下来?

如您所见,将 Swiper 集成到您的网站或应用程序中非常容易。以下是您的后续步骤