Thumbnail Carousel
Introduction
A carousel can sync with another carousel by sync()
method,
and isNaviation
option makes the carousel clickable for navigation.
By using these 2 features, we can make a carousel with a thumbnail control like this:
This page explains how to create this kind of carousel.
Thumbnail Carousel
Let's start with the thumbnail carousel.
<
section
id
=
"thumbnail-carousel"
class
=
"splide"
aria-label
=
"The carousel with thumbnails. Selecting a thumbnail will change the Beautiful Gallery carousel."
>
<
div
class
=
"splide__track"
>
<
ul
class
=
"splide__list"
>
<
li
class
=
"splide__slide"
>
<
img
src
=
"thumbnail01.jpg"
alt
=
""
>
<
/
li
>
<
li
class
=
"splide__slide"
>
<
img
src
=
"thumbnail02.jpg"
alt
=
""
>
<
/
li
>
<
li
class
=
"splide__slide"
>
<
img
src
=
"thumbnail03.jpg"
alt
=
""
>
<
/
li
>
<
/
ul
>
<
/
div
>
<
/
section
>
HTML
<section id="thumbnail-carousel" class="splide" aria-label="The carousel with thumbnails. Selecting a thumbnail will change the Beautiful Gallery carousel." > <div class="splide__track"> <ul class="splide__list"> <li class="splide__slide"> <img src="thumbnail01.jpg" alt=""> </li> <li class="splide__slide"> <img src="thumbnail02.jpg" alt=""> </li> <li class="splide__slide"> <img src="thumbnail03.jpg" alt=""> </li> </ul> </div> </section>
The thumbnail carousel have to display multiple slides in a page.
Normally, we use the perPage
option to define the number of slides,
but it's hard to determine the number from the perspective of responsibility even if using breakpoints.
To solve this problem, let's use fixedWidth
to fix the slide width against the window size.
document
.
addEventListener
(
'DOMContentLoaded'
,
function
(
)
{
new
Splide
(
'#thumbnail-carousel'
,
{
fixedWidth
:
100
,
gap
:
10
,
rewind
:
true
,
pagination
:
false
,
}
)
.
mount
(
)
;
}
)
;
JavaScript
document.addEventListener( 'DOMContentLoaded', function () { new Splide( '#thumbnail-carousel', { fixedWidth: 100, gap : 10, rewind : true, pagination: false, } ).mount(); } );
And then, we need to fit the thumbnail to the slide dimension. If all thumbnails have the same aspect ratio, the following CSS is sufficient:
.splide__slide
img
{
width
:
100
%
;
height
:
auto
;
}
CSS
.splide__slide img { width: 100%; height: auto; }
If not, let's use object-fit
to cover up each slide with a thumbnail (IE does not support this property!).
To make it work, we need to set fixedHeight
:
document
.
addEventListener
(
'DOMContentLoaded'
,
function
(
)
{
new
Splide
(
'#thumbnail-carousel'
,
{
fixedWidth
:
100
,
fixedHeight
:
60
,
gap
:
10
,
rewind
:
true
,
pagination
:
false
,
}
)
.
mount
(
)
;
}
)
;
JavaScript
document.addEventListener( 'DOMContentLoaded', function () { new Splide( '#thumbnail-carousel', { fixedWidth : 100, fixedHeight: 60, gap : 10, rewind : true, pagination : false, } ).mount(); } );
...and apply the property to thumbnails:
.splide__slide
img
{
width
:
100
%
;
height
:
100
%
;
object-fit
:
cover
;
}
CSS
.splide__slide img { width: 100%; height: 100%; object-fit: cover; }
Now the carousel will be like this:
It looks pretty good, but there are a few things we should consider:
- The user cannot tell which slide is selected
100px
is probably big for mobile devices- The active slide should be on the center if possible
If you are using the theme CSS, such as splide.min.css
or splide-skyblue.min.css
,
it emphasizes the active slide by adding a border style.
Otherwise, you have to style it manually by the is-active
status class:
.splide__slide
{
opacity
:
0.6
;
}
.splide__slide.is-active
{
opacity
:
1
;
}
CSS
.splide__slide { opacity: 0.6; } .splide__slide.is-active { opacity: 1; }
The breakpoints
and focus
options help us to deal with the second and third problems.
If you are not using object-fit
, fixedHeight
is not necessary.
new
Splide
(
'#thumbnail-carousel'
,
{
fixedWidth
:
100
,
fixedHeight
:
60
,
gap
:
10
,
rewind
:
true
,
pagination
:
false
,
focus
:
'center'
,
breakpoints
:
{
600
:
{
fixedWidth
:
60
,
fixedHeight
:
44
,
}
,
}
,
}
)
.
mount
(
)
;
JavaScript
new Splide( '#thumbnail-carousel', { fixedWidth : 100, fixedHeight: 60, gap : 10, rewind : true, pagination : false, focus : 'center', breakpoints: { 600: { fixedWidth : 60, fixedHeight: 44, }, }, } ).mount();
Next, we need to make slides clickable by setting isNavigation
to true
:
new
Splide
(
'#thumbnail-carousel'
,
{
fixedWidth
:
100
,
fixedHeight
:
60
,
gap
:
10
,
rewind
:
true
,
pagination
:
false
,
isNavigation
:
true
,
breakpoints
:
{
600
:
{
fixedWidth
:
60
,
fixedHeight
:
44
,
}
,
}
,
}
)
.
mount
(
)
;
JavaScript
new Splide( '#thumbnail-carousel', { fixedWidth : 100, fixedHeight : 60, gap : 10, rewind : true, pagination : false, isNavigation: true, breakpoints : { 600: { fixedWidth : 60, fixedHeight: 44, }, }, } ).mount();
Note that the thumbnail carousel should not have pagination, or otherwise accessibility tree will be kind of messed up.
As a result the carousel looks like this. Click a thumbnail, and you will see the carousel moves:
To ignore small drag by a mouse, use the dragMinThreshold
option.
Main Carousel
This tutorial uses the following fade carousel.
You have to assign a different ID, such as main-carousel
, or a class to grab it later by script.
This example disables arrows and pagination, but if you want to enable them, simply delete these options.
Sync
To sync the main carousel to thumbnails, use the Splide#sync()
method.
You must call it before mount()
.
var
main
=
new
Splide
(
'#main-carousel'
,
{
...
}
)
;
var
thumbnails
=
new
Splide
(
'#thumbnail-carousel'
,
{
...
}
)
;
main
.
sync
(
thumbnails
)
;
main
.
mount
(
)
;
thumbnails
.
mount
(
)
;
JavaScript
var main = new Splide( '#main-carousel', { ... } ); var thumbnails = new Splide( '#thumbnail-carousel', { ... } ); main.sync( thumbnails ); main.mount(); thumbnails.mount();
That's all! 🎊 Here is the whole code of this tutorial:
123456789101112131415161718192021222324252627document
.
addEventListener
(
'DOMContentLoaded'
,
function
(
)
{
var
main
=
new
Splide
(
'#main-carousel'
,
{
type
:
'fade'
,
rewind
:
true
,
pagination
:
false
,
arrows
:
false
,
}
)
;
var
thumbnails
=
new
Splide
(
'#thumbnail-carousel'
,
{
fixedWidth
:
100
,
fixedHeight
:
60
,
gap
:
10
,
rewind
:
true
,
pagination
:
false
,
isNavigation
:
true
,
breakpoints
:
{
600
:
{
fixedWidth
:
60
,
fixedHeight
:
44
,
}
,
}
,
}
)
;
main
.
sync
(
thumbnails
)
;
main
.
mount
(
)
;
thumbnails
.
mount
(
)
;
}
)
;
JavaScript
document.addEventListener( 'DOMContentLoaded', function () { var main = new Splide( '#main-carousel', { type : 'fade', rewind : true, pagination: false, arrows : false, } ); var thumbnails = new Splide( '#thumbnail-carousel', { fixedWidth : 100, fixedHeight : 60, gap : 10, rewind : true, pagination : false, isNavigation: true, breakpoints : { 600: { fixedWidth : 60, fixedHeight: 44, }, }, } ); main.sync( thumbnails ); main.mount(); thumbnails.mount(); } );