Methods

Embla Carousel provides a set of methods that let you extend and control the carousel programmatically.


Usage

Methods require an initialized carousel instance. They remain accessible while the instance is active and are invalid after calling destroy.

Calling methods

In the following example, the slideNodes method is called and logged to the console as soon as the carousel has been initialized:

import EmblaCarousel from 'embla-carousel'
const wrapperNode = document.querySelector('.embla')const viewportNode = wrapperNode.querySelector('.embla__viewport')
const emblaApi = EmblaCarousel(viewportNode, { loop: true })
console.log(emblaApi.slideNodes())
<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 EmblaCarouselType is obtained directly from the core package embla-carousel and used like so:

import EmblaCarousel, { EmblaCarouselType } from 'embla-carousel'
const wrapperNode = <HTMLElement>document.querySelector('.embla')const viewportNode = <HTMLElement>wrapperNode.querySelector('.embla__viewport')
const emblaApi = EmblaCarousel(viewportNode, { loop: true })
const logSelectedSnap = (emblaApi: EmblaCarouselType): void => {  console.log(emblaApi.selectedScrollSnap())}
emblaApi.on('select', logSelectedSnap)
<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 methods with their respective parameters and return values.


rootNode

emblaApi.rootNode()

Get the root node that holds the scroll container with slides inside. This method can be useful when you need to manipulate the root element dynamically or similar.


containerNode

emblaApi.containerNode()

Get the container node that holds the slides. This method can be useful when you need to manipulate the container element dynamically or similar.


slideNodes

emblaApi.slideNodes()

Get all the slide nodes inside the container. This method can be useful when you need to manipulate the slide elements dynamically or similar.


scrollNext

emblaApi.scrollNext(true | false)

Scroll to the next snap point if possible. When loop is disabled and the carousel has reached the last snap point, this method won't do anything. Set the jump parameter to true when you want to go to the next slide instantly.


scrollPrev

emblaApi.scrollPrev(true | false)

Scroll to the previous snap point if possible. When loop is disabled and the carousel has reached the first snap point, this method won't do anything. Set the jump parameter to true when you want to go to the previous slide instantly.


scrollTo

emblaApi.scrollTo(0, true | false)

Scroll to a snap point by its unique index. If loop is enabled, Embla Carousel will choose the closest way to the target snap point. Set the jump parameter to true when you want to go to the desired slide instantly.


canScrollNext

emblaApi.canScrollNext()

Check the possiblity to scroll to a next snap point. If loop is enabled and the container holds any slides, this will always return true.


canScrollPrev

emblaApi.canScrollPrev()

Check the possiblity to scroll to a previous snap point. If loop is enabled and the container holds any slides, this will always return true.


selectedScrollSnap

emblaApi.selectedScrollSnap()

Get the index of the selected snap point.


previousScrollSnap

emblaApi.previousScrollSnap()

Get the index of the previously selected snap point.


scrollSnapList

emblaApi.scrollSnapList()

Get an array containing all the snap point positions. Each position represents how far the carousel needs to progress in order to reach this position.


scrollProgress

emblaApi.scrollProgress()

Check how far the carousel has scrolled of its scrollable length from 0 - 1. For example, 0.5 equals 50%. For example, this can be useful when creating a scroll progress bar.


slidesInView

emblaApi.slidesInView()

Get slide indexes visible in the carousel viewport. Honors the inViewThreshold option.


slidesNotInView

emblaApi.slidesNotInView()

Get slide indexes not visible in the carousel viewport. Honors the inViewThreshold option.


internalEngine

emblaApi.internalEngine()

Exposes almost all internal functionality used by Embla. Useful when creating plugins or similar.

Note: Please refrain from creating bug reports related to this method. If you're using this and running into problems, it's a 99.8% chance that you don't understand how this works. Use at your own risk.


reInit

emblaApi.reInit({ loop: false }, [Autoplay()])

Hard reset the carousel after it has been initialized. This method allows for changing options and plugins after initializing a carousel.

Note: Passed options will be merged with current options, but passed plugins will replace current plugins.


plugins

emblaApi.plugins()

Returns an object with key value pairs where the keys are the plugin names, and the plugin API:s are the values.


destroy

emblaApi.destroy()

Destroy the carousel instance permanently. This is a one way operation and is intended to be used as a cleanup measure when the carousel instance isn't needed anymore.


on

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

Subscribe to an Embla specific event with a callback. Added event listeners will persist even if reInit is called, either until the carousel is destroyed or the event is removed with the off method.


off

emblaApi.off('select', (emblaApi, event) => {})

Unsubscribe from an Embla specific event . Make sure to pass the same callback reference when the callback was added with the on method.


emit

emblaApi.emit('select')

Emits an embla event. This doesn't trigger any internal Embla functionality.


Edit this page on GitHub