Overview
A swiper is a .nds-swiper-wrapper of .nds-swiper-slide items, which scroll and snap with CSS. A slide holds any content, usually a Card or an image. The optional navigation row holds the previous and next buttons and the pagination bullets, which the script builds. A hero swiper fills a Hero section, and a spotlight keeps one slide in the middle with smaller slides at its sides.
Pick another component when:
- every item must be visible at once: Grid
- each item is a separate view the user picks by name: Tabs
- the list is long and the user searches or pages through it: Pagination
Markup
Variants
A Per view choice has one row for each knob it changes: write them all in the swiper’s style. The Hero and Spotlight structures always show one slide, so Per view stays at 1 on them. Peek and Loading are off on a hero. Peek, Loop and Loading are off on a spotlight, which sizes and loops itself.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Cards (default) | — | — | A row of cards or images, several on a page. The usual swiper |
| Structure | Hero (demo: + per-1) | canon #swiper-hero |
— | Full-width slides with a background image, one at a time, at the top of a page |
| Structure | Spotlight (demo: + per-1) (hint: One slide at full size, smaller ones at its sides) | canon #swiper-spotlight |
— | One slide in the middle at full size, with smaller slides at its sides. For a set of images or cards the user looks at one by one |
| Per view | 3 · 2 · 1 (default) (hint: Slides on desktop, tablet and phone) | — | .nds-swiper:not(.nds-hero):not(.nds-spotlight) |
3 slides on a desktop, 2 on a tablet, 1 on a phone |
| Per view | 4 · 3 · 2 (hint: Slides on desktop, tablet and phone) | --max-slides: 4 |
.nds-swiper:not(.nds-hero):not(.nds-spotlight) |
Smaller items, such as logos or short cards |
| Per view | 4 · 3 · 2 (hint: Slides on desktop, tablet and phone) | --mid-slides: 3 |
.nds-swiper:not(.nds-hero):not(.nds-spotlight) |
The same, on a tablet |
| Per view | 4 · 3 · 2 (hint: Slides on desktop, tablet and phone) | --min-slides: 2 |
.nds-swiper:not(.nds-hero):not(.nds-spotlight) |
The same, on a phone |
| Per view | 1 (id: per-1) | --max-slides: 1 |
.nds-swiper:not(.nds-hero):not(.nds-spotlight) |
One slide at every width, such as an image gallery |
| Per view | 1 (id: per-1) | --mid-slides: 1 |
.nds-swiper:not(.nds-hero):not(.nds-spotlight) |
The same, on a tablet |
| Per view | 1 (id: per-1) | — | .nds-swiper.nds-hero |
A hero always shows one slide. It needs no knob |
| Per view | 1 (id: per-1) | — | .nds-swiper.nds-spotlight |
A spotlight always shows one slide in the middle. It needs no knob |
| Arrows | Beside bullets (default) | — | — | The arrows at the start of the navigation row and the bullets at its end |
| Arrows | Split (hint: Bullets in the middle, an arrow at each end) | .nds-center |
.nds-swiper-navigation |
The bullets in the middle of the row and one arrow at each end |
| Arrows | Middle (hint: An arrow on each side of the slides, no bullets) | .nds-middle |
.nds-swiper |
One arrow on each side of the slides, at their middle, and no bullets, so the Bullets size has no effect (not on a hero). Tablet and wider: a phone shows the navigation row |
| Bullets | LG (default) | — | — | 16px bullets. It needs no class |
| Bullets | MD | .nds-md |
.nds-swiper-pagination |
12px bullets |
| Bullets | SM | .nds-sm |
.nds-swiper-pagination |
8px bullets |
| Peek | Peek (hint: Shows the edge of the next slide) | --peek: 40px |
.nds-swiper:not(.nds-hero):not(.nds-spotlight) |
Shows 40px of the next slide, so the user sees there is more. Any length works |
| Loop | Loop | [data-swiper-loop] |
.nds-swiper:not(.nds-spotlight) |
An endless row: the first slide follows the last |
| Autoplay | Autoplay | [data-swiper-autoplay="5000"] |
.nds-swiper:is([data-swiper-loop], .nds-spotlight) |
The row moves by itself every 5000 ms. It needs Loop, or a spotlight, which always loops |
| Pause button | Pause button | canon #swiper-pause | .nds-swiper:not(.nds-hero) .nds-swiper-buttons |
A secondary button that pauses and plays the row, with a ring that counts down to the next move. It needs Autoplay |
| Pause button | Pause button | canon #swiper-pause-hero | .nds-hero .nds-swiper-buttons |
The same button at the size of the hero arrows |
| Pause button | Pause button | — | .nds-swiper[data-swiper-autoplay] |
Only on a row with autoplay |
| Loading | Loading (hint: Skeleton placeholders) | .nds-loading |
.nds-swiper:not(.nds-hero):not(.nds-spotlight) |
Every card shows as a skeleton while its data loads. Remove the class when the data is in |
Behavior
Slides per View
--max-slides, --mid-slides and --min-slides set how many slides show on a desktop, a tablet and a phone. CSS sizes the slides from them before the script runs, so the row does not move when the script starts. The arrows and the bullets move one page, and a page is that many slides. The last page ends at the last slide, so it can share slides with the page before it.
Peek
--peek leaves that length of the next slide showing at the end of each page. The slides shrink to make room for it. When all the slides fit on one page, the script turns the peek off, since there is nothing to show.
Loop
data-swiper-loop makes the row endless in both directions. The script copies slides to both ends. Screen readers skip the copies and Tab never lands on them, but a click on one works. When the row stops on a copy, it jumps to the real slide with no visible move. The swiper ignores the attribute unless it has more slides than its largest slides per view. A loop never turns its arrows off, and it has no at-start or at-end state.
Autoplay
data-swiper-autoplay moves a looping row one page at a time by itself. Its value is the wait in milliseconds, 5000 if you leave it empty. It needs data-swiper-loop, or a spotlight, which always loops. With no loop the row does not move, so a swiper with too few slides to loop has no autoplay. The row waits while the pointer is on it, a finger touches it, keyboard focus is in it, the tab is hidden or it is off screen. A user who asks for reduced motion gets a paused row. The optional nds-pause button stops and starts the row, and its pause icon turns to play while the row is paused. With nds-progress its ring counts down to the next move and stops whenever the row waits. The wait then goes on from where it stopped. Each new slide gets the full wait, half a second after it moves in. Pause empties the ring, and play goes on to the next slide at once.
Middle Arrows
nds-middle moves the arrows out of the navigation row to each side of the slides, at their middle, and hides the bullets. The swiper makes room for the arrows, so they never cover a slide. Use it for a wide strip the user scans, such as a row of partner logos. On a phone the navigation row comes back, since the user swipes there. On a hero, the arrows sit over the slide and the bullets stay.
Hero
nds-hero shows one full-width slide at a time, with no gap, inside a Hero section. Put nds-full-width on the swiper, so the slides reach the edges of the screen. The navigation row sits over the bottom of the slides. Write hidden on every slide after the first. The script shows those slides when the hero first comes into view, so the browser does not decode their images before then.
Spotlight
nds-spotlight keeps the open slide in the middle at full size. The slides at its sides are smaller and sit close to it, and they grow as they move to the middle. It always loops, and each arrow, bullet or swipe moves one slide. On a wide row a whole slide shows at each side. On a narrow row the open slide stays at least 240px wide, and less of the side slides shows. Content wider than that makes its slide wider, and the slide still stops in the middle. It needs three slides to loop. It ignores the slides per view, --peek, --padding and data-swiper-loop.
Built-in Features
-
Auto-initialization
The script starts every
nds-swiperon the page. There is nothing to call. -
Native Scroll Snap
The row scrolls and snaps with CSS, so a swipe, a drag and a trackpad feel native. The row still scrolls when the script has not loaded.
-
Stable First Paint
The slides have their final width before the script runs, and the navigation row keeps its space. Nothing on the page moves when the swiper starts.
-
Page-Count Navigation
The script makes one bullet for each page, and makes them again when a new screen width changes the page count. When all the slides fit on one page, it hides the arrows and the bullets.
-
Lazy Images
An image with
data-srcordata-srcsetloads when its slide comes within 200px of the screen. When the images on screen finish loading, the slides one page before and after them load ahead at low priority, so the next slide is ready before it moves in. Until then, its box shows a skeleton. -
Keyboard Navigation
The swiper takes focus. The arrow keys move one page, in the reading direction. Home goes to the first page and End to the last.
-
Nested Swipers
A slide can hold another swiper. Each swiper uses only its own arrows and bullets, and the keys move the one that has focus.
-
Programmatic Control
A script can move a swiper to any slide, and the swiper fires
nds:swiper:changeafter every move.
Best Practices
- Use a swiper for a set the user browses, such as featured services or a gallery. Put content the user must see all at once in a Grid.
- Write
hiddenon.nds-swiper-navigation. The script shows the row only when there is more than one page. - Set the slides per view in the swiper’s
style, not in a stylesheet. The script reads them fromstyle. - Slides that arrive after the swiper started: call
NDS.init.destroy(swiper), append the slides, thenNDS.init.mount(swiper). A refresh does not add slides to a running swiper. - Give the slides in one swiper the same height. The tallest slide sets the height of the row.
- Load the images of later slides with
data-srcanddata-srcset, notsrc. Give the first imagefetchpriority="high". - Give a lazy image a width and a height, or an
aspect-ratio. Before it loads it has no size of its own, so its skeleton does not show. - In a section, put the swiper in a breakout body: a section body with
nds-breakout, after the wrapper. The row then runs to the edges of the page. See Section. - Add
--peekwhen the slides do not fit on one page, so the user sees there is more. - Keep a hero to four slides or fewer. Few users reach the later ones.
- Put one element in each spotlight slide, such as an image or a card. The spotlight shrinks and grows that one element.
- With
nds-middleon a hero, keep the slide text away from the sides, or center it. The arrows sit over the edges of the slide. - Give each arrow an
aria-labelthat names its direction. - Always write
data-swiper-loopwithdata-swiper-autoplay. - WCAG asks for a way to stop content that moves by itself. Add the
nds-pausebutton if your site must meet it. - Keep the autoplay wait at 5000 ms or more, so the user can read a slide before it moves.
- Do not use autoplay on a row the user must read in order, such as steps or a form.
API
Other Classes
| Class | Element | Effect |
|---|---|---|
nds-oncolor |
.nds-swiper |
Light bullets, arrows and section text, for a dark or image background. The Hero structure has it. Every part inside takes its on-color look, the arrows included. Inside an on-color area (data-theme="dark oncolor"), the swiper already takes this look: skip the class |
nds-swiper-buttons |
div in .nds-swiper-navigation |
Holds the previous and next buttons |
nds-pause |
.nds-btn |
Makes the button pause and play an autoplay row. Write the pause icon in it: it turns to play while paused. Add nds-progress and the ring for the countdown. Style it as any button. It can sit anywhere in the swiper. The script hides it when the row has no autoplay |
nds-prev, nds-next |
.nds-btn |
Makes the button move one page, and gives it its arrow icon. Style it as any button. The buttons can sit anywhere in the swiper |
nds-bullet |
button |
A bullet. The script makes them in .nds-swiper-pagination |
nds-swiper-clone |
.nds-swiper-slide |
The script writes it on each copy it makes for a loop |
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-swiper-loop |
.nds-swiper |
An endless row. See Loop under Behavior |
data-swiper-autoplay |
.nds-swiper |
The wait between moves in milliseconds, 5000 when empty. It needs a loop. See Autoplay under Behavior |
data-src, data-srcset |
img, source in a slide |
The image source, loaded when the slide comes near the screen |
hidden |
.nds-swiper-navigation, hero slides after the first |
Hides them until the script shows them |
aria-pressed |
.nds-pause |
The script writes true when the row is paused by the button, pause() or reduced motion, and false on play |
data-state |
.nds-swiper |
The script writes at-start on the first page and at-end on the last, and both when there is one page. Use them to style the ends: .nds-swiper[data-state~="at-end"] |
data-swiper-clone |
.nds-swiper-clone |
The index of the real slide the copy repeats. Use it to update the copies of a slide you change at runtime |
data-state |
.nds-bullet, spotlight .nds-swiper-slide |
The script writes active on the current bullet. In a spotlight it also writes active on the open slide and its loop copies, and after on the slides after it |
data-swiper-peek |
.nds-swiper |
The script writes it while the peek shows |
CSS Custom Properties
Set these in the style of .nds-swiper. Set the bullet colors on the swiper or on a parent. Do not set the --swiper-gap, --swiper-padding, --swiper-peek, --swiper-slides or --swiper-total values: the swiper computes them from these.
| Property | Default | Controls |
|---|---|---|
--max-slides |
1 |
Slides per view on a desktop: 960px and wider |
--mid-slides |
1 |
Slides per view on a tablet: 600px to 959px |
--min-slides |
1 |
Slides per view on a phone: narrower than 600px |
--peek |
unset | Length of the next slide left showing, such as 40px. 0px is no peek |
--gap |
var(--spacing-xl) |
Space between slides |
--padding |
0px |
Space at each end of the row. In a breakout body it is the page padding, so the row runs to the screen edge and its first slide lines up with the content above it |
--total |
1 |
Number of slides. The script writes it |
--progress-duration |
the autoplay wait | Length of the ring on .nds-pause. The script writes it from data-swiper-autoplay |
--swiper-bullet-default |
var(--swiper-bullet-background-default) |
Bullet color |
--swiper-bullet-default-hovered |
var(--swiper-bullet-background-hovered) |
Bullet color on hover |
--swiper-bullet-active |
var(--swiper-bullet-background-active) |
Color of the current bullet |
--swiper-bullet-active-hovered |
var(--swiper-bullet-background-active-hovered) |
Color of the current bullet on hover |
--swiper-bullet-border |
transparent |
Bullet border color |
Tokens
Set a token at :root, or on a wrapper to reach every swiper inside it. Give a token with a Dark mode value a dark override too: see Tokens.
Source: the swiper group in _sass/tokens/_components.scss.
| Token | Preview | Value |
|---|---|---|
--swiper-bullet-background-default | var(--colors-neutral-200)Dark mode var(--colors-neutral-700) | |
--swiper-bullet-background-hovered | var(--colors-neutral-300)Dark mode var(--colors-neutral-600) | |
--swiper-bullet-background-active | var(--colors-primary-600)Dark mode var(--colors-primary-500) | |
--swiper-bullet-background-active-hovered | var(--colors-primary-700)Dark mode var(--colors-primary-600) | |
--swiper-bullet-background-default-oncolor | var(--colors-alpha-white-60) | |
--swiper-bullet-background-hovered-oncolor | var(--colors-alpha-white-80) | |
--swiper-bullet-background-active-oncolor | var(--colors-alpha-white-100) | |
--swiper-bullet-background-active-hovered-oncolor | var(--colors-alpha-white-80) |
It also reads these shared tokens. A change to one also changes other components.
Source: the var() reads in _sass/components/_swiper.scss.
| Token | Preview | Value |
|---|---|---|
--text-oncolor-primaryRead by color, --btn-loading-color | var(--colors-base-white) | |
--text-default-oncolor-disabledRead by --btn-color-disabled | var(--colors-alpha-white-40) | |
--button-background-transparent-hoveredRead by --btn-bg-hover | var(--colors-alpha-white-20) | |
--button-background-transparent-pressedRead by --btn-bg-active | var(--colors-alpha-white-40) | |
--button-background-transparent-selectedRead by --btn-bg-selected | var(--colors-alpha-white-30) | |
--button-label-transparent-oncolor-defaultRead by --btn-color | var(--text-oncolor-primary) |
JavaScript
The NDS.Swiper script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.
A swiper in the page starts by itself. The instance is on the element as el.ndsSwiper. Indexes count real slides only, never loop copies.
A swiper far down the page sets up when its section is about to show, so the page loads faster. Until then it has no dots and no data-nds-swiper-initialized. A method call sets it up at once.
| Method | Effect |
|---|---|
NDS.Swiper.init() |
Starts every .nds-swiper that has not started |
NDS.Swiper.create(el) |
Starts one swiper and returns its instance, or the instance it already has. Returns null when the swiper has no wrapper or no slides |
NDS.Swiper.destroy(el) |
Stops one swiper and puts its markup back as written. NDS.init.destroy() calls it |
instance.next(), instance.prev() |
Moves one page |
instance.goTo(index, { instant }) |
Moves to a slide, no further than the last page. A looping swiper has no last page, so it moves to any slide. instant: true moves at once |
instance.play(), instance.pause() |
Starts and stops an autoplay row, as the nds-pause button does. They do nothing on a row with no autoplay |
instance.realIndex |
The current slide. Loop copies are not counted |
instance.wrapper, instance.currentIndex, instance.lastIndex, instance.slidesPerView |
Read-only state. currentIndex counts loop copies |
instance.toRealIndex(index) |
Returns the slide of a row index. A row index counts loop copies |
instance.syncState() |
Updates the buttons, bullets and classes to the current slide |
instance.goToFull(index, { instant }) |
Moves to a row index, which counts loop copies. instant: true moves at once. Every move runs through it |
instance.destroy() |
The same as NDS.Swiper.destroy(el) |
| Event | Fired on | Detail |
|---|---|---|
nds:swiper:change |
.nds-swiper, and bubbles |
index: the slide at the start of the page, or the open slide in a spotlight, after every move |
The full API is in the banner of _js/nds-swiper.js.