Skip to main content
Login

Sign in to access your account

Swiper - National Design System

A swiper is a horizontal row of slides that the user swipes, scrolls or pages through with arrows and bullets.

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

Options
Structure
Per view
Arrows
Bullets
More
Preview
Passport Renewal

Renew a passport online and collect it from the nearest office.

Vehicle Registration

Register a new vehicle or transfer its ownership.

Business License

Apply for a commercial license and track the request.

National Address

Register or update the national address of a home.

Traffic Violations

View traffic violations and pay them in one step.

Appointments

Book, change or cancel an appointment at a service center.

<div class="nds-swiper" style="--max-slides: 3; --mid-slides: 2; --min-slides: 1"> <div class="nds-swiper-wrapper"> <div class="nds-swiper-slide"> <div class="nds-card nds-stroke"> <div class="nds-card-content"> <span class="nds-card-title">Passport Renewal</span> <p class="nds-card-description">Renew a passport online and collect it from the nearest office.</p> </div> </div> </div> <div class="nds-swiper-slide"> <div class="nds-card nds-stroke"> <div class="nds-card-content"> <span class="nds-card-title">Vehicle Registration</span> <p class="nds-card-description">Register a new vehicle or transfer its ownership.</p> </div> </div> </div> <div class="nds-swiper-slide"> <div class="nds-card nds-stroke"> <div class="nds-card-content"> <span class="nds-card-title">Business License</span> <p class="nds-card-description">Apply for a commercial license and track the request.</p> </div> </div> </div> <div class="nds-swiper-slide"> <div class="nds-card nds-stroke"> <div class="nds-card-content"> <span class="nds-card-title">National Address</span> <p class="nds-card-description">Register or update the national address of a home.</p> </div> </div> </div> <div class="nds-swiper-slide"> <div class="nds-card nds-stroke"> <div class="nds-card-content"> <span class="nds-card-title">Traffic Violations</span> <p class="nds-card-description">View traffic violations and pay them in one step.</p> </div> </div> </div> <div class="nds-swiper-slide"> <div class="nds-card nds-stroke"> <div class="nds-card-content"> <span class="nds-card-title">Appointments</span> <p class="nds-card-description">Book, change or cancel an appointment at a service center.</p> </div> </div> </div> </div> <div class="nds-swiper-navigation" hidden> <div class="nds-swiper-buttons"> <button type="button" class="nds-btn nds-primary nds-icon-only nds-circle nds-md nds-prev" aria-label="Previous slide" data-i18n-aria-label="swiper.previous_slide"></button> <button type="button" class="nds-btn nds-primary nds-icon-only nds-circle nds-md nds-next" aria-label="Next slide" data-i18n-aria-label="swiper.next_slide"></button> </div> <div class="nds-swiper-pagination"></div> </div> </div>

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-swiper on 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-src or data-srcset loads 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:change after 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 hidden on .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 from style.
  • Slides that arrive after the swiper started: call NDS.init.destroy(swiper), append the slides, then NDS.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-src and data-srcset, not src. Give the first image fetchpriority="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 --peek when 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-middle on 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-label that names its direction.
  • Always write data-swiper-loop with data-swiper-autoplay.
  • WCAG asks for a way to stop content that moves by itself. Add the nds-pause button 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.

TokenPreviewValue
--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-oncolorvar(--colors-alpha-white-60)
--swiper-bullet-background-hovered-oncolorvar(--colors-alpha-white-80)
--swiper-bullet-background-active-oncolorvar(--colors-alpha-white-100)
--swiper-bullet-background-active-hovered-oncolorvar(--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.

TokenPreviewValue
--text-oncolor-primary
Read by color, --btn-loading-color
var(--colors-base-white)
--text-default-oncolor-disabled
Read by --btn-color-disabled
var(--colors-alpha-white-40)
--button-background-transparent-hovered
Read by --btn-bg-hover
var(--colors-alpha-white-20)
--button-background-transparent-pressed
Read by --btn-bg-active
var(--colors-alpha-white-40)
--button-background-transparent-selected
Read by --btn-bg-selected
var(--colors-alpha-white-30)
--button-label-transparent-oncolor-default
Read 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
var el = document.querySelector('.nds-swiper'); var swiper = el.ndsSwiper; swiper.goTo(3); // animated swiper.goTo(0, { instant: true }); // at once // Change something outside the swiper with the slide el.addEventListener('nds:swiper:change', function (e) { console.log('slide', e.detail.index); });

The full API is in the banner of _js/nds-swiper.js.

Last Modified Date: 20/11/2026 - 05:15 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks