Events

Embla Carousel provides a set of events you can listen to for reacting to carousel updates and user interactions.


Usage

Events are available only on an initialized carousel instance. They fire throughout the carousel's lifecycle, and any added listeners remain active even after a hard reset with the reInit method.

Adding event listeners

After initializing the carousel, you can subscribe to events. The following example shows how to listen for the slidesInView event:

import EmblaCarousel from 'embla-carousel'
const wrapperNode = document.querySelector('.embla')const viewportNode = wrapperNode.querySelector('.embla__viewport')
const emblaApi = EmblaCarousel(viewportNode, { loop: true })
const logSlidesInView = (emblaApi) => {  console.log(emblaApi.slidesInView())}
emblaApi.on('slidesInView', logSlidesInView)
<div class="embla">  <div class="embla__viewport">    <div class="embla__container">      <div class="embla__slide">Slide 1</div>      <div class="embla__slide">Slide 2</div>      <div class="embla__slide">Slide 3</div>    </div>  </div></div>

Removing event listeners

To remove an event listener, call the off method and pass the same callback reference that was originally used with on.

import EmblaCarousel from 'embla-carousel'
const wrapperNode = document.querySelector('.embla')const viewportNode = wrapperNode.querySelector('.embla__viewport')
const emblaApi = EmblaCarousel(viewportNode, { loop: true })
const logSlidesInViewOnce = (emblaApi) => {  console.log(emblaApi.slidesInView())  emblaApi.off('slidesInView', logSlidesInViewOnce)}
emblaApi.on('slidesInView', logSlidesInViewOnce)
<div class="embla">  <div class="embla__viewport">    <div class="embla__container">      <div class="embla__slide">Slide 1</div>      <div class="embla__slide">Slide 2</div>      <div class="embla__slide">Slide 3</div>    </div>  </div></div>

TypeScript

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

import EmblaCarousel, {  EmblaCarouselType,  EmblaEventType} from 'embla-carousel'
const wrapperNode = <HTMLElement>document.querySelector('.embla')const viewportNode = <HTMLElement>wrapperNode.querySelector('.embla__viewport')
const emblaApi = EmblaCarousel(viewportNode, { loop: true })
const logEmblaEvent = (  emblaApi: EmblaCarouselType,  eventName: EmblaEventType): void => {  console.log(`Embla just triggered ${eventName}!`)}
emblaApi.on('select', logEmblaEvent)
<div class="embla">  <div class="embla__viewport">    <div class="embla__container">      <div class="embla__slide">Slide 1</div>      <div class="embla__slide">Slide 2</div>      <div class="embla__slide">Slide 3</div>    </div>  </div></div>

Reference

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


init

emblaApi.on('init', (emblaApi) => {})

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

emblaApi.on('reInit', (emblaApi) => {})

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

emblaApi.on('destroy', (emblaApi) => {})

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

emblaApi.on('select', (emblaApi) => {})

/>

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


scroll

emblaApi.on('scroll', (emblaApi) => {})

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

emblaApi.on('settle', (emblaApi) => {})

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

emblaApi.on('resize', (emblaApi) => {})

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


slidesInView

emblaApi.on('slidesInView', (emblaApi) => {})

/>

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

emblaApi.on('slidesChanged', (emblaApi) => {})

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


slideFocus

emblaApi.on('slideFocus', (emblaApi) => {})

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


pointerDown

emblaApi.on('pointerDown', (emblaApi) => {})

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


pointerUp

emblaApi.on('pointerUp', (emblaApi) => {})

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