davidjerleke / embla-carousel

A lightweight carousel library with fluid motion and great swipe precision.
https://www.embla-carousel.com
MIT License
6.21k stars 187 forks source link

Ability to jumpTo (or similar) to slide instead of scrollTo #176

Closed simon-mundy closed 3 years ago

simon-mundy commented 3 years ago

Feature request is related to

Is your feature request related to an issue?

Describe the solution you'd like

davidjerleke commented 3 years ago

Hi Simon (@simon-mundy),

Thank you for your feature request.

There are situations where I would like to create a Carousel in a modal overlay and jump to a specific slide rather than scrolling/animating to it.

Have you tried using the startIndex option? Usage looks like this:

VanillaJs

const embla = EmblaCarousel(emblaNode, { startIndex: 2 })

ReactJs

const embla = useEmblaCarousel({ startIndex: 2 })

You can also make use of the reInit method like so:

VanillaJs or ReactJs

embla.reInit({ startIndex: 2 })

If we're to add this feature to the Embla core, I think we need to add it to the following methods:

One way to do it would be to add a boolean as a second argument that triggers an instant scroll. The default value for this will be false:

embla.scrollTo(2, true) // Scrolls instantly 

Let me know what you think.

Best, David

davidjerleke commented 3 years ago

Hello Simon (@simon-mundy),

I've been wanting to add this feature to Embla Carousel myself so I just added it and released v4.4.0, which has this feature. Please try it out and let me know if it's working as intended.

Best, David

simon-mundy commented 3 years ago

Amazing! Thankyou @davidcetinkaya will give it a try ASAP

davidjerleke commented 3 years ago

Great @simon-mundy. Let me know if it’s working as intended.

davidjerleke commented 3 years ago

@simon-mundy closing this as resolved. Feel free to open it if you run into any issues.

Best, David