Swiper Svelte is available only via NPM as a part of the main Swiper library:
npm i swiper
swiper/svelte
exports 2 components: Swiper
and SwiperSlide
:
<Swiper
spaceBetween={50}
slidesPerView={3}
on:slideChange={() => console.log('slide change')}
on:swiper={(e) => console.log(e.detail[0])}
>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
<SwiperSlide>Slide 4</SwiperSlide>
...
</Swiper>
<script>
// Import Swiper Svelte components
import { Swiper, SwiperSlide } from 'swiper/svelte';
// Import Swiper styles
import 'swiper/css';
</script>
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 <Swiper
modules={[Navigation, Pagination, Scrollbar, A11y]}
spaceBetween={50}
slidesPerView={3}
navigation
pagination={{ clickable: true }}
scrollbar={{ draggable: true }}
on:slideChange={() => console.log('slide change')}
on:swiper={(e) => console.log(e.detail[0])}
>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
<SwiperSlide>Slide 4</SwiperSlide>
...
</Swiper>
<script>
// import Swiper core and required modules
import { Navigation, Pagination, Scrollbar, A11y } from 'swiper';
import { Swiper, SwiperSlide } from 'swiper/svelte';
// Import Swiper styles
import 'swiper/css';
import 'swiper/css/navigation';
import 'swiper/css/pagination';
import 'swiper/css/scrollbar';
</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
Svelte component receive all Swiper parameters as component props.
<Swiper
slidesPerView="{3}"
spaceBetween="{50}"
navigation
pagination
...
></Swiper>
Swiper component supports all Swiper events, including additional swiper
event that returns swiper instance as soon as posible.
event.detail
. <Swiper
on:swiper={onSwiper}
on:slideChange={() => console.log('slide change')}
on:progress={onProgress}
>
...
</Swiper>
<script>
import { Swiper, SwiperSlide } from 'swiper/svelte';
/* "progress" event emitted with "swiper" and "progress" argumentts */
const onProgress = (e) => {
const [swiper, progress] = e.detail;
console.log(progress);
}
/* "swiper" event emitted with "swiper" instance argument */
const onSwiper = (e) => {
const [swiper] = e.detail;
console.log(swiper);
}
</script>
Prop | Type | Default | Description |
---|---|---|---|
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 receives the following data
object:
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>
<SwiperSlide let:data="{{ isActive }}">
<div>Current slide is { isActive ? 'active' : 'not active' }</div>
</SwiperSlide>
</Swiper>
Swiper Svelte uses "slots" for content distribution. There are 4 slots available
container-start
- element will be added to the beginning of swiper-containercontainer-end
- 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>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<span slot="container-start">Container Start</span>
<span slot="container-end">Container End</span>
<span slot="wrapper-start">Wrapper Start</span>
<span slot="wrapper-end">Wrapper End</span>
</Swiper>
Will be rendered as:
<div class="swiper">
<span slot="container-start">Container Start</span>
<div class="swiper-wrapper">
<span slot="wrapper-start">Wrapper Start</span>
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<span slot="wrapper-end">Wrapper End</span>
</div>
<span slot="container-end">Container End</span>
</div>
To implement Virtual Slides we need to:
pass array with slides to Swiper's virtual.slides
property
render slides based on virtualData
slot's data. It is also mandatory to pass virtualIndex
. virtualData
is the object with following properties:
offset
- slides left/top offset in pxfrom
- index of first slide required to be renderedto
- index of last slide required to be renderedslides
- slides to render <Swiper
modules={[Virtual]}
spaceBetween={50}
slidesPerView={3}
virtual={{ slides: virtualSlides }}
let:virtualData={{ slides, offset, from }}
>
{#each slides as slide, index (from + index)}
<SwiperSlide
virtualIndex={from + index}
style={`left: ${offset}px`}
>{slide}</SwiperSlide>
{/each}
</Swiper>
<script>
import { Virtual } from 'swiper';
import { Swiper, SwiperSlide } from 'swiper/svelte';
// Create array with 1000 slides
const virtualSlides = Array.from({ length: 1000 }).map((el, index) => `Slide ${index + 1}`);
</script>
Controller requires to pass one Swiper instance to another:
<!-- Main Swiper -> pass controlled swiper instance -->
<Swiper
modules={[Controller]}
controller={{ control: controlledSwiper }}
...
>
<!-- ... -->
</Swiper>
<!-- Controlled Swiper -> store swiper instance -->
<Swiper
modules={[Controller]}
on:swiper={setControlledSwiper}
...
>
<!-- ... -->
</Swiper>
<script>
import { Controller } from 'swiper';
import { Swiper, SwiperSlide } from 'swiper/svelte';
// store Controller swiper instance
let controlledSwiper = null;
const setControlledSwiper = (e) => {
const [swiper] = e.detail;
// set Controller swiper instance
setTimeout(() => {
controlledSwiper = swiper;
});
};
</script>
For two-way control (when both Swipers control each other) it should be like this:
<Swiper
modules={[Controller]}
on:swiper={setFirstSwiper}
controller={{ control: secondSwiper }}
...
>
<!-- ... -->
</Swiper>
<Swiper
modules={[Controller]}
on:swiper={setSecondSwiper}
controller={{ control: firstSwiper }}
...
>
<!-- ... -->
</Swiper>
<script>
import SwiperCore, { Controller } from 'swiper';
import { Swiper, SwiperSlide } from 'swiper/svelte';
// store swiper instances
let firstSwiper = null;
let secondSwiper = null;
const setFirstSwiper = (e) => {
const [swiper] = e.detail;
setTimeout(() => {
firstSwiper = swiper;
});
};
const setSecondSwiper = (e) => {
const [swiper] = e.detail;
setTimeout(() => {
secondSwiper = swiper;
});
};
</script>
Same as with controller we need to store thumbs instance and pass it to main gallery:
<!-- 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]}
watchSlidesProgress
on:swiper={setThumbsSwiper}
...
>
<!-- ... -->
</Swiper>
<script>
import { Swiper, SwiperSlide } from 'swiper/svelte';
import SwiperCore, { Thumbs } from 'swiper';
// store Thumbs swiper instance
let thumbsSwiper = null;
const setThumbsSwiper = (e) => {
const [swiper] = e.detail;
// set Thumbs swiper instance
setTimeout(() => {
thumbsSwiper = swiper;
});
};
</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.
<Swiper modules="{[EffectFade]}" effect="fade">
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
</Swiper>
<script>
import { Swiper, SwiperSlide } from 'swiper/svelte';
import { EffectFade } from 'swiper';
import 'swiper/css';
import 'swiper/css/effect-fade';
</script>
As you see it is really easy to integrate Swiper into your website or app. So here are your next steps: