Variable slidesToScroll #198
-
|
Hey there, Thanks for this fantastic carousel, it's been great to work with. I'm currently using the carousel for a project, but am running into an issue and wasn't sure if I'd missed an option somewhere. I have a carousel with multiple fixed width slides (fixed at 200px width). The carousel is on a responsive site, so you see a different number of slides depending on screen size. For example, on mobile you can see only 1-2 items, while on larger desktops you can see up to 5. When you click the 'next' button to scroll the carousel, it only scrolls 1 slide at a time. You can use the This means before rendering the carousel, I need to:
If the screen size changes, I need to recalculate the visible size and then Have I missed something? Is there a way of scrolling a visible page of elements built in? Thanks for any help! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
Hi @ravenaxiom, Thank you for your question. The
.embla__slide {
flex: 0 0 100%; /* Default slide covers 100% of the viewport */
}
@media (min-width: 768px) {
.embla__slide {
flex: 0 0 50%; /* Breakpoint SM slide covers 50% of the viewport */
}
}
.embla:before {
display: none;
content: '{
"slidesToScroll": 1 /* Scroll 1 slide as default */
}';
}
@media (min-width: 768px) {
.embla:before {
content: '{
"slidesToScroll": 2 /* Scroll 2 slides SM up */
}';
}
}The example above is taken from the Breakpoints guide in the docs. But judging from your description:
It seems like you prefer fixed slide sizes for your use case. Assuming all slides have the same width, you can solve it like so: const options = { inViewThreshold: 1 }
const embla = EmblaCarousel(emblaNode, options)When inViewThreshold is set to const setSlidesToScrollToVisibleCount = (embla) => {
return () => {
const slidesToScroll = embla.slidesInView().length
embla.reInit({ slidesToScroll })
}
}And how to use it: const setSlidesToScroll = setSlidesToScrollToVisibleCount(embla)
embla.on('init', setSlidesToScroll)
embla.on('resize', setSlidesToScroll)Let me know if it helps. Best, |
Beta Was this translation helpful? Give feedback.
Hi @ravenaxiom,
Thank you for your question. The
slidesToScrolloption will group slides into the given integer. It won't do any checks if the slides fit into the viewport or any other checks for that matter. So as you mention, you'll have to write that logic yourself if you want it to be handled automatically.slidesToScrollis easier to use with relative slide sizes which basically means setting you slide sizes in%. Let's say you want to scroll2slides on desktop and1on mobile, you can do it like this: