Events

Embla Carousel exposes events that you can listen to in order to react to changes in the carousel.


Usage

You need an initialized carousel in order to make use of events. Events will only be fired during the lifecycle of a carousel and added event listeners will persist even when you hard reset the carousel with the reInit method.

Adding event listeners

After initializing a carousel, we're going to subscribe to the slidesInView event in the following example:

import EmblaCarousel from 'embla-carousel'
const emblaNode = document.querySelector('.embla')const emblaApi = EmblaCarousel(emblaNode)
function logSlidesInView(emblaApi) {  console.log(emblaApi.slidesInView())}
emblaApi.on('slidesInView', logSlidesInView)

Removing event listeners

In order to remove an event listener, you'll have to call the off method and make sure to pass the same callback reference you passed to the on method:

import EmblaCarousel from 'embla-carousel'
const emblaNode = document.querySelector('.embla')const emblaApi = EmblaCarousel(emblaNode)
function logSlidesInViewOnce(emblaApi) {  console.log(emblaApi.slidesInView())  emblaApi.off('slidesInView', logSlidesInViewOnce)}
emblaApi.on('slidesInView', logSlidesInViewOnce)

TypeScript

The EmblaEventType is obtained directly from the core package embla-carousel and used like so:

import EmblaCarousel, {  EmblaCarouselType,  EmblaEventType} from 'embla-carousel'
const emblaNode = document.querySelector('.embla')const emblaApi = EmblaCarousel(emblaNode)
function logEmblaEvent(  emblaApi: EmblaCarouselType,  eventName: EmblaEventType): void {  console.log(`Embla just triggered ${eventName}!`)}
emblaApi.on('slidesInView', logEmblaEvent)

Reference

Below follows an exhaustive list of all Embla Carousel events together with information about how they work.


init

Once: yes

Runs when the carousel mounts for the first time. This only fires once which means that it won't fire when the carousel is re-initialized using the reInit method.


reInit

Once: no

Runs when the reInit method is called. When the window is resized, Embla Carousel automatically calls the reInit method which will also fire this event.


destroy

Once: yes

Runs when the carousel has been destroyed using the destroy method. This only fires once and will be the last event the carousel fires.


select

Once: no

Runs when the selected scroll snap changes. The select event is triggered by drag interactions or the scrollNext, scrollPrev or scrollTo methods.


scroll

Once: no

Runs when the carousel is scrolling. It might be a good idea to throttle this if you're doing expensive stuff in your callback function.


settle

Once: no

Runs when the carousel has settled after scroll has been triggered. Please note that this can take longer than you think when dragFree is enabled or when using slow transitions.


resize

Once: no

Runs when the carousel container or the slide sizes change. It's using ResizeObserver under the hood.


slidesInView

Once: no

Runs when any slide has entered or exited the viewport. This event is intended to be used together with the slidesInView and/or slidesNotInView methods.


slidesChanged

Once: no

Runs when slides are added to, or removed from the carousel container. It's using MutationObserver under the hood.


slideFocus

Once: no

Runs when a slide receives focus. For example, when a focusable element like a button, link or input receives focus inside a slide.


pointerDown

Once: no

Runs when the user has a pointer down on the carousel. It's triggered by a touchstart or a mousedown event.


pointerUp

Once: no

Runs when the user has released the pointer from the carousel. It's triggered by a touchend or a mouseup event.


Edit this page on GitHub