Carousel
Display and cycle through a collection of items.
Projects /
Display and cycle through a collection of items.
The Carousel component can be used to cycle through a series of items (cards, images, etc.), with the option of displaying multiple items at the same time.
You can edit the following data attributes to customize the carousel behavior:
data-drag="on"
to the js-carousel
element (default is off);data-autoplay="on"
to the js-carousel
element (default is off). Autoplay is automatically paused when user hovers over the carousel element, interacts with the carousel contents or the carousel controls;data-autoplay-interval
equal to the custom duration (in milliseconds) that you want to use (default is 5000). This option is effective only if data-autoplay
is on;data-autoplay-hover="on"
to the js-carousel
element (default is off). This option is effective only if data-autoplay
is on; data-autoplay-focus="on"
to the js-carousel
element (default is off). This option is effective only if data-autoplay
is on; data-loop="off"
to the js-carousel
element (default is on);data-navigation="on"
(default is off); this option is available when data-loop="off"
.The number of items visible in a carousel cycle is determined based on:
.carousel__item
as defined in the carousel.scss file. To modify this value, you can change the --carousel-item-auto-size
CSS variable (default is 260px);--carousel-grid-gap
CSS variable defined inside the carousel.scss file (default is var(--space-xs)
).If you want to use the Carousel component to create a slideshow (one single item visible at a time), you can:
--carousel-item-auto-size
CSS variable equal to 100%;--carousel-grid-gap
CSS variable equal to zero;For example, define your custom carousel class:
.carousel--slideshow {
--carousel-item-auto-size: 100%;
--carousel-grid-gap: 0;
}
and apply it to your carousel element:
<div class="carousel carousel--slideshow js-carousel">
<!-- carousel content here -->
</div>
To add a navigation to the carousel, add a data-navigation="on"
attribute to the .js-carousel
element (applicable if data-loop="off"
):
<div class="carousel js-carousel" data-loop="off" data-navigation="on">
<!-- carousel content here -->
</div>
You can customize the carousel navigation using the following data attributes:
data-navigation-class
: pass a list of custom classes to be added to the navigation element (default is carousel__navigation
);data-navigation-item-class
: pass a custom class for the navigation list items (default is carousel__nav-item
). When an item is selected, the class {navigation-item-class}--selected
is added to that element (e.g., if data-navigation-item-class='carousel__navigation-item'
, the selected class will be carousel__navigation-item--selected
);data-navigation-pagination
: set it to 'on' to use the carousel navigation as a pagination element (see --pagination variation demo).You can add a counter to your carousel (see --counter variation demo): add the .js-carousel__counter
and the .js-carousel__counter-tot
classes to the counter and the total elements respectively:
<div class="carousel js-carousel">
<!-- carousel content here -->
<span class="js-carousel__counter"></span>/<span class="js-carousel__counter-tot"></span>
</div>
If data-loop="on"
(default option), the Carousel plugin removes carousel items and reinsert clones of those items when required. For this reason, custom JS code applied to the carousel items (e.g., event listeners) may not work after a loop has been completed.
In this case, we suggest to set the data-loop
option to off.
In the cases where you still need to have the infinite loop, you can solve this problem re-initializing your custom code when new clones are created. Use the carousel-updated
event to target new clone items:
var carousel = document.getElementsByClassName('js-carousel')[0];
carousel.addEventListener('carousel-updated', function(event){
// reinitialize your custom js here
// event.detail -> list of clone items
});
For example, if your carousel item includes a slideshow, you would need:
var carousel = document.getElementsByClassName('js-carousel')[0];
carousel.addEventListener('carousel-updated', function(event){
// event.detail -> list of clone items
for(var i = 0; i < event.detail.length; i++) {
var slideshow = event.detail[i].getElementsByClassName('js-slideshow');
if(slideshow.length > 0) {
new Slideshow({element: slideshow[0]});
}
}
});
If the carousel items number is smaller than the number of visible items, and you want them to be centered in the viewport, add a data-justify-content="on"
to the .carousel
item. By default items will be aligned to the left:
<div class="carousel flex flex-column js-carousel" data-loop="off" data-justify-content="on">
<!-- carousel content -->
</div>
Each time a new group of carousel items are reveled, the carousel-active-items
custom event is emitted. This event can be used to detect the first visible item in the carousel, and the total number of visible items:
var carousel = document.getElementsByClassName('js-carousel')[0];
carousel.addEventListener('carousel-active-items', function(event){
// event.detail.firstSelectedItem -> first visible item
// event.detail.visibleItemsNb -> number of visible items
});
If your carousel content is created dynamically, you may need to initialize it (once it has been added to the page) to make it work properly.
Once the dynamic content has been added to the page, initialize it using the Carousel
object:
var carouselEl = document.getElementsByClassName('carousel')[0];
new Carousel({
element: carouselEl,
autoplay : false, // enable/disable autoplay
autoplayInterval: false, // in milliseconds - default is 5000 (5s)
loop: false, // enable/disable infinite loop
nav: true, // add carousel dots navigation
drag: false // enable/disable drag
});