Swiper Vue.js plugin is available only via NPM as a part of the main Swiper library:
npm i swiper
swiper/vue
exports 2 components: Swiper
and SwiperSlide
:
<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>
Here is the list of additional modules imports:
Virtual
- Virtual Slides moduleKeyboard
- Keyboard Control moduleMousewheel
- Mousewheel Control moduleNavigation
- Navigation modulePagination
- Pagination moduleScrollbar
- Scrollbar moduleParallax
- Parallax moduleFreeMode
- Free Mode moduleGrid
- Grid moduleManipulation
- Slides manipulation module (only for Core version)Zoom
- Zoom moduleLazy
- Lazy moduleController
- Controller moduleA11y
- Accessibility moduleHistory
- History Navigation moduleHashNavigation
- Hash Navigation moduleAutoplay
- Autoplay moduleEffectFade
- Fade Effect moduleEffectCube
- Cube Effect moduleEffectFlip
- Flip Effect moduleEffectCoverflow
- Coverflow Effect moduleEffectCards
- Cards Effect moduleEffectCreative
- Creative Effect moduleThumbs
- Thumbs module<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';
// 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.nextEl
, pagination.el
, etc.)Swiper package contains different sets of CSS, Less and SCSS styles:
swiper/css
- only core Swiper stylesswiper/css/bundle
- all Swiper styles including all modules styles (like Navigation, Pagination, etc.)Modules styles (not required if you already imported bundle styles):
swiper/css/a11y
- styles required for A11y moduleswiper/css/autoplay
- styles required for Autoplay moduleswiper/css/controller
- styles required for Controller moduleswiper/css/effect-cards
- styles required for Cards Effect moduleswiper/css/effect-coverflow
- styles required for Coverflow Effect moduleswiper/css/effect-creative
- styles required for Creative Effect moduleswiper/css/effect-cube
- styles required for Cube Effect moduleswiper/css/effect-fade
- styles required for Fade Effect moduleswiper/css/effect-flip
- styles required for Flip Effect moduleswiper/css/free-mode
- styles required for Free Mode moduleswiper/css/grid
- styles required for Grid moduleswiper/css/hash-navigation
- styles required for Hash Navigation moduleswiper/css/history
- styles required for History moduleswiper/css/keyboard
- styles required for Keyboard moduleswiper/css/lazy
- styles required for Lazy moduleswiper/css/manipulation
- styles required for Manipulation moduleswiper/css/mousewheel
- styles required for Mousewheel moduleswiper/css/navigation
- styles required for Navigation moduleswiper/css/pagination
- styles required for Pagination moduleswiper/css/parallax
- styles required for Parallax moduleswiper/css/scrollbar
- styles required for Scrollbar moduleswiper/css/thumbs
- styles required for Thumbs moduleswiper/css/virtual
- styles required for Virtual moduleswiper/css/zoom
- styles required for Zoom moduleFor Less styles replace css
with less
in imports paths, e.g.:
import 'swiper/less';
import 'swiper/less/navigation';
import 'swiper/less/pagination';
For SCSS styles replace css
with scss
in imports paths, e.g.:
import 'swiper/scss';
import 'swiper/scss/navigation';
import 'swiper/scss/pagination';
Swiper
Vue.js component receive all Swiper parameters as component props, plus some extra props:
Prop | Type | Default | Description |
---|---|---|---|
tag | string | 'div' | Main Swiper container HTML element tag |
wrapperTag | string | 'div' | Swiper wrapper HTML element tag |
Swiper component supports all Swiper events, including additional swiper
event that returns swiper instance as soon as possible. For example:
<swiper @swiper="..." @slideChange="..." @reachEnd="..." ...></swiper>
Prop | Type | Default | Description |
---|---|---|---|
tag | string | 'div' | Swiper Slide HTML element tag |
zoom | boolean | false | Enables additional wrapper required for zoom mode |
virtualIndex | number | Actual swiper slide index. Required to be set for virtual slides |
SwiperSlide
component has the following slot props:
isActive
- true when current slide is activeisPrev
- true when current slide is the previous from activeisNext
- true when current slide is the next from activeisVisible
- true when current slide is visible (watchSlidesProgress
Swiper parameter must be enabled)isDuplicate
- true when current slide is a duplicate slide (when loop
mode enabled)
For example:<swiper>
<swiper-slide v-slot="{ isActive }">
<div>Current slide is {{ isActive ? 'active' : 'not active' }}</div>
</swiper-slide>
</swiper>
Swiper Vue provides useSwiper
hook to easliy get the Swiper instance in components inside of 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
is one more hook for components inside of Swiper slides to get the slide data (same data as in SwiperSlide slot 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 component uses "slots" for content distribution. There are 4 slots available
container-start
- element will be added to the beginning of swiper-containercontainer-end
(default) - element will be added to the end of swiper-containerwrapper-start
- element will be added to the beginning of swiper-wrapperwrapper-end
- element will be added to the end of swiper-wrapperFor example:
<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>
Will be rendered as:
<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 wnd</span>
</div>
<span>Container end</span>
</div>
Virtual Slides rendering here is fully handled by Vue.js and not required anything except setting :virtual="true"
property and setting virtualIndex
on slides:
<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';
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>
Controller requires to pass one Swiper instance to another:
<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';
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>
For two-way control (when both Swipers control each other) it should be like this:
<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';
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>
Same as with controller we need to store thumbs instance and pass it to main gallery:
<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';
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>
The following effects are available:
- Fade
- Cube
- Coverflow
- Flip
- Cards
- Creative
To use effects you have to import and install them first (as all other modules).
You can find running effect demos here.
<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';
import { Swiper, SwiperSlide } from 'swiper/vue';
import 'swiper/css';
import 'swiper/css/effect-fade';
export default {
components: {
Swiper,
SwiperSlide,
},
setup() {
return {
EffectFade,
};
},
};
</script>
As you see it is really easy to integrate Swiper into your website or app. So here are your next steps:
Go to API Documentation to learn more about all Swiper API and how to control it.
If you have questions about Swiper ask them in StackOverflow or Swiper Discussions.
Create issue on GitHub if you found a bug.
If you are looking for support, we have a private Discord support chat room for Swiper Patrons.