Package Exports
- embla-carousel
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (embla-carousel) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Version 3 is here! Please read the migration guide here. It may take some time to update the documentation so bare with me. Cheers!
Embla Carousel
Extensible bare bone carousels for the web. Build awesome carousels by extending them with your own CSS and JavaScript. Embla Carousel is dependency free and 100% open source.
Installation
NPM
npm install embla-carousel
QuickStart
HTML
<div class="embla">
<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 class="embla__slide">
Slide 4
</div>
</div>
</div>CSS
.embla {
overflow: hidden;
}
.embla__container {
display: flex;
}
.embla__slide {
position: relative; /* Needed if loop: true */
flex: 0 0 100%; /* Choose any slide width */
}JavaScript
import EmblaCarousel from 'embla-carousel'
const emblaNode = document.querySelector('.embla')
const options = { loop: true }
const embla = EmblaCarousel(emblaNode, options)Options
Configure Embla by passing an options object as the second argument. Default values are:
const embla = EmblaCarousel(emblaNode, {
align: 'center',
containerSelector: '*',
slidesToScroll: 1,
containScroll: false,
draggable: true,
dragFree: false,
loop: false,
speed: 10,
startIndex: 0,
selectedClass: 'is-selected',
draggableClass: 'is-draggable',
draggingClass: 'is-dragging',
})align
Align the slides relative to the carousel viewport.
start, center and end, you can provide a number to align the slides. For example, if you pass 0.2, slides will be aligned 20% from the viewport start edge. Note that slide alignments will be overrided for slides at the start and end when used together with
containScroll
,
that prevents excessive scrolling at the beginning or end.
string | number
start
center
end
number
center
Usage
const options = { align: 'start' }
const embla = EmblaCarousel(emblaNode, options)containerSelector
Target the slide container with a query selector.
string
any
*
Usage
const options = { containerSelector: '.my-container-selector' }
const embla = EmblaCarousel(emblaNode, options)<div class="embla">
<div class="my-container-selector">
...slides
</div>
</div>slidesToScroll
Scroll past the given number of slides.
2, every two slides will be treated as a single slide.
number
any
1
Usage
const options = { slidesToScroll: 2 }
const embla = EmblaCarousel(emblaNode, options).embla__slide {
flex: 0 0 50%; /* Show 2 slides in viewport */
}containScroll
Contain slides to the carousel viewport.
boolean
true
false
false
Usage
const options = { containScroll: true }
const embla = EmblaCarousel(emblaNode, options)draggable
Allow drag interactions to scroll the carousel.
boolean
true
false
true
Usage
const options = { draggable: false }
const embla = EmblaCarousel(emblaNode, options)dragFree
Enable momentum scrolling for drag interactions.
boolean
true
false
false
Usage
const options = { dragFree: true }
const embla = EmblaCarousel(emblaNode, options)loop
Enable infinite looping for the carousel.
containScroll
will be ignored if loop is enabled because the empty space is already filled with looping slides.
boolean
true
false
false
Usage
const options = { loop: true }
const embla = EmblaCarousel(emblaNode, options).embla__slide {
position: relative; /* Needed for loop to work */
}speed
Set scroll speed triggered by API navigation.
scrollNext
,
scrollPrev
and
scrollTo
. Use a higher number for faster scrolling. Drag interactions are not affected by this because the speed in these cases is determined by how vigorous the drag gesture was.
number
any
10
Usage
const options = { speed: 15 }
const embla = EmblaCarousel(emblaNode, options)startIndex
Select index of the initial scroll snap.
0. If slides are mapped to groups with the
slidesToScroll
option, some slides share the same scroll snap index. For example, if it's set to 2 slide one and two will be at index 0, while slide three and four will be at index 1 and so on.
number
any
0
Usage
const options = { startIndex: 3 }
const embla = EmblaCarousel(emblaNode, options)selectedClass
Choose classname applied to the selected slides.
slidesToScroll
is more than 1 and/or
containScroll
is active, slides are mapped to groups. This means that the selected class will be added to multiple slides at a time.
string
any
is-selected
Usage
const options = { selectedClass: 'my-selected-class' }
const embla = EmblaCarousel(emblaNode, options)draggableClass
Choose classname applied to the draggable container.
draggable
. Use it to style the carousel accordingly. For example, you can show a grab cursor when a draggable carousel is hovered. If no value is provided it will fall back to is-draggable.
string
any
is-draggable
Usage
const options = { draggableClass: 'my-draggable-class' }
const embla = EmblaCarousel(emblaNode, options).my-draggable-class {
cursor: grab;
}draggingClass
Choose classname applied to the container when dragging.
draggable
. Use it to style the carousel accordingly. For example, you can show a grabbing cursor when a pointer is down. If no value is provided it will fall back to is-dragging.
string
any
is-dragging
Usage
const options = { draggingClass: 'my-dragging-class' }
const embla = EmblaCarousel(emblaNode, options).my-dragging-class {
cursor: grabbing;
}API
Embla exposes API methods that can be used to control the carousel externally. Example usage:
embla.scrollNext()
embla.scrollTo(2)
embla.changeOptions({ loop: true })
embla.on('select', () => {
console.log(`Selected snap index is ${embla.selectedScrollSnap()}.`)
})containerNode
Get the container node that holds the slides.
containerSelector option this will return the matching element, otherwise it will return the first immediate child of the Embla node passed to EmblaCarousel.
none
Node.ELEMENT_NODE
Usage
const embla = EmblaCarousel(emblaNode, options)
const emblaContainer = embla.containerNode()slideNodes
Get the slide nodes inside the container.
none
Node.ELEMENT_NODE[]
Usage
const embla = EmblaCarousel(emblaNode, options)
const emblaSlides = embla.slideNodes()scrollNext
Scroll to the next snap point if possible.
loop
option is disabled and the carousel is on the last snap point, this method will do nothing. When loop is enabled, it will always be able to scroll to the next snap point. Useful for creating a scroll next button for example.
none
undefined
Usage
const embla = EmblaCarousel(emblaNode, options)
const nextButton = emblaNode.querySelector('.embla__next')
nextButton.addEventListener('click', embla.scrollNext, false)<button class="embla__next" type="button">
Scroll Next
</button>scrollPrev
Scroll to the previous snap point if possible.
loop
option is disabled and the carousel is on the first snap point, this method will do nothing. When loop is enabled, it will always be able to scroll to the previous snap point. Useful for creating a scroll previous button for example.
none
undefined
Usage
const embla = EmblaCarousel(emblaNode, options)
const prevButton = emblaNode.querySelector('.embla__prev')
prevButton.addEventListener('click', embla.scrollPrev, false)<button class="embla__prev" type="button">
Scroll Previous
</button>scrollTo
Scroll to a snap point by its unique index.
loop
option is enabled, the carousel will seek the closest way to the target. Useful for creating dot navigation together with the
scrollSnapList
method.
index: number
undefined
Usage
const embla = EmblaCarousel(emblaNode, options)
const rewindButton = emblaNode.querySelector('.embla__rewind')
rewindButton.addEventListener('click', () => embla.scrollTo(0), false)<button class="embla__rewind" type="button">
Rewind
</button>scrollToProgress
Scroll the carousel by to given location.
scrollProgress
from 0 to 1 by directly setting it. For example, assuming that the carousel is positioned on the first snap point, 0.5 will scroll the carousel half of its scrollable length. Scroll to target is smooth. The second parameter allows for snapping the carousel to the closest snap point based on the target scroll progress (note that this will alter the desired progress a bit in order to snap it).
progress: number
snap: boolean
undefined
Usage
const embla = EmblaCarousel(emblaNode, options)
embla.scrollToProgress(0.5)scrollBy
Scroll the carousel by the given amount.
scrollProgress
from 0 to 1 by either adding to it or subtracting from it. For example, assuming that the carousel is positioned on the first snap point, 0.5 will scroll the carousel half of its scrollable length. Scroll to target is smooth. The second parameter allows for snapping the carousel to the closest snap point based on the target scroll progress (note that this will alter the desired progress a bit in order to snap it).
progress: number
snap: boolean
undefined
Usage
const embla = EmblaCarousel(emblaNode, options)
embla.scrollBy(0.5)canScrollPrev
Check the possiblity to scroll to a previous snap point.
loop
option is enabled it will always return true. For example, it can be used to disable or enable a scroll to previous button.
none
boolean
Usage
const embla = EmblaCarousel(emblaNode, options)
const prevButton = emblaNode.querySelector('.embla__prev')
const togglePrevButtonEnabled = () => {
if (embla.canScrollPrev()) {
prevButton.removeAttribute('disabled')
} else {
prevButton.setAttribute('disabled', 'disabled')
}
}
embla.on('init', togglePrevButtonEnabled)
embla.on('select', togglePrevButtonEnabled)<button class="embla__prev" type="button">
Scroll Previous
</button>canScrollNext
Check the possiblity to scroll to a next snap point.
loop
option is enabled it will always return true. For example, it can be used to disable or enable a scroll to next button.
none
boolean
Usage
const embla = EmblaCarousel(emblaNode, options)
const nextButton = emblaNode.querySelector('.embla__next')
const toggleNextButtonEnabled = () => {
if (embla.canScrollNext()) {
nextButton.removeAttribute('disabled')
} else {
nextButton.setAttribute('disabled', 'disabled')
}
}
embla.on('init', toggleNextButtonEnabled)
embla.on('select', toggleNextButtonEnabled)<button class="embla__next" type="button">
Scroll Next
</button>selectedScrollSnap
Get the index of the selected snap point.
slidesToScroll
option is more than 1 some slides will be grouped together and share the same index. For example, when it's set to 2, every two slides will share the same index. In this case, slide 1 and 2 will share index 0 and slide 3 and 4 will share index 1 and so on.
none
number
Usage
const embla = EmblaCarousel(emblaNode, options)
embla.on('select', () => {
const currentSnapIndex = embla.selectedScrollSnap()
alert(`Selected index has changed to ${currentSnapIndex}.`)
})previousScrollSnap
Get the index of the previous snap point.
slidesToScroll
option is more than 1 some slides will be grouped together and share the same index. For example, when it's set to 2, every two slides will share the same index. In this case, slide 1 and 2 will share index 0 and slide 3 and 4 will share index 1 and so on.
none
number
Usage
const embla = EmblaCarousel(emblaNode, options)
embla.on('select', () => {
const previousSnapIndex = embla.previousScrollSnap()
alert(`Previously selected index was ${previousSnapIndex}.`)
})scrollSnapList
Get an array of all scroll snap points.
slideNodes and slideIndexes. For example, it's useful for getting the snap point count or creating a dot navigation together with the
scrollTo
method.
none
scrollSnap[]
Usage
const embla = EmblaCarousel(emblaNode, options)
const scrollSnaps = embla.scrollSnapList()
const slidesInFirstScrollSnap = scrollSnaps[0].slideNodes
const indexesInFirstScrollSnap = scrollSnaps[0].slideIndexesscrollProgress
Check how far the carousel has scrolled.
0 at the beginning to 1 at the end. For example, it's useful for creating a progress bar together with the
scroll
event. When invoking scrollProgress without the target parameter, the carousel returns the scroll progress of its current location. However, if you want to grab the target scroll progress the target parameter has to be true.
target: boolean
number
Usage
const embla = EmblaCarousel(emblaNode, options)
embla.on('scroll', () => {
const scrollPercentage = embla.scrollProgress() * 100
console.log(`The carousel has scrolled ${scrollPercentage}%.`)
})clickAllowed
Check if interaction was a static click.
true if a drag interaction in any direction didn't occur before the mouse was released. Touch events also require the carousel to not be in a scrolling state in order to accept the click.
none
boolean
Usage
const embla = EmblaCarousel(emblaNode, options)
const emblaSlides = embla.slideNodes()
const alertClickedSlide = index => {
return () => {
if (embla.clickAllowed()) {
alert(`Slide with index ${index} was clicked.`)
}
}
}
emblaSlides.forEach((slide, index) => {
const alertClickedSlideIndex = alertClickedSlide(index)
slide.addEventListener('click', alertClickedSlideIndex, false)
})changeOptions
Change the carousel options after initialization.
options:
EmblaOptions
undefined
Usage
const embla = EmblaCarousel(emblaNode, options)
embla.changeOptions({ loop: true })destroy
Destroy a carousel instance permanently.
none
undefined
Usage
const embla = EmblaCarousel(emblaNode, options)
embla.destroy()on
Subscribe to an Embla specific event with a callback.
events
. For example, it's useful for changing styles whenever a new target snap point has been selected or when the carousel is scrolling. Use it together with the
off
method to remove added event listeners without destroying the carousel. However, when the
destroy
method is invoked, any added event listeners will be destroyed.
event: EmblaEvent
callback: function
undefined
Usage
const embla = EmblaCarousel(emblaNode, options)
const onInitCallback = () => {
console.log('The carousel is ready to rock.')
}
embla.on('init', onInitCallback)off
Unsubscribe from an Embla specific event.
events
. It's useful for removing added event listeners without destroying the carousel. Note that you don't have to remove event listeners added using the
on
method when invoking
destroy
, because it will destroy all added event listeners for you.
event: EmblaEvent
callback: function
undefined
Usage
const embla = EmblaCarousel(emblaNode, options)
const logIndex = () => {
const selectedIndex = embla.selectedScrollSnap()
console.log(`Selected index has changed to ${selectedIndex}.`)
}
const addLogIndexListener = () => embla.on('select', logIndex)
const removeLogIndexListener = () => embla.off('select', logIndex)Events
Embla exposes custom events that can be hooked on to. Example usage:
embla.on('select', () => {
console.log(`Selected snap index is ${embla.selectedScrollSnap()}.`)
})
embla.on('scroll', () => {
console.log(`Scroll progress is ${embla.scrollProgress()}.`)
})init
Fire a callback when the carousel mounts.
API
is ready to use. Note that the init event only fires once upon the first initialisation and won't trigger when invoking
changeOptions
or similar.
Usage
const embla = EmblaCarousel(emblaNode, options)
const onInitCallback = () => {
console.log('The carousel is ready to rock.')
})
embla.on('init', onInitCallback)destroy
Fire a callback when the carousel is destroyed.
Usage
const embla = EmblaCarousel(emblaNode, options)select
Fire a callback when selected scroll snap changes.
Usage
const embla = EmblaCarousel(emblaNode, options)scroll
Fire a callback when the carousel is scrolling.
Usage
const embla = EmblaCarousel(emblaNode, options)
embla.on('scroll', () => {
const scrollPercentage = embla.scrollProgress() * 100
console.log(`The carousel has scrolled ${scrollPercentage}%.`)
})settle
Fire a callback when the carousel has settled.
Usage
const embla = EmblaCarousel(emblaNode, options)
embla.on('settle', () => {
console.log(`The carousel has stopped scrolling.`)
})resize
Fire a callback when the carousel has resized.
Usage
const embla = EmblaCarousel(emblaNode, options)dragStart
Under construction...
Usage
const embla = EmblaCarousel(emblaNode, options)dragEnd
Under construction...
Usage
const embla = EmblaCarousel(emblaNode, options)CodeSandbox
Get started instantly with one of the CodeSandboxes below.
Basic Setup
- With Previous, Next & Dot buttons.
Autoplay
- Example of how to setup Autoplay.
Browser Support
Embla has been tested in the browsers listed below. Special thanks goes to BrowserStack.
IE - 11
Edge - Latest 2 versions
Chrome - Latest 2 versions
Firefox - Latest 2 versions
Safari - Latest 2 versions
Contributors
Thank you to all contributors for making Embla Carousel awesome! Contributions are welcome.
Open Source
Copyright © 2019-present, David Cetinkaya.
Embla is MIT licensed 💖
· · ·