Closed vajkri closed 2 years ago
Hi @vajkri,
Thank you for your question. By default, the slidesInView
method returns the slides in view at the current carousel scroll location when the method is called, not the target/destination. This is useful when you want fine grain control. For example, if you want to track slides in view as the carousel is scrolling, you can attach it to the scroll
event. But I think you’re looking for the target
parameter:
true
when you want to get the slides visible at the scroll destination instead of the current scroll location.An example:
function logSlidesInView() {
const slidesInView = embla.slidesInView(true); // <-- Pass true to the slidesInView method
console.log(slidesInView);
}
embla.on('select', logSlidesInView);
embla.on('pointerUp', logSlidesInView);
Note that I added pointerUp
event because if you have a carousel with dragFree enabled, dragging might not change the selected snap at all times, but it could change which slides are in view and not.
Let me know if it helps.
Best, David
Perfect, thanks your for quick reply. Passing true
solved it for me :-)
@davidjerleke This answer suggests that slidesInView
takes a target argument that can be true or false. That does not seem to be in the current API. See https://github.com/davidjerleke/embla-carousel/blob/master/packages/embla-carousel/src/components/EmblaCarousel.ts#L213
Bug is related to
Embla Carousel version
Describe the bug
slidesInView
(and actually alsoslidesNotInView
) seem to be one select event too late. Both with single and multiple items in view.CodeSandbox
Steps to reproduce
slidesInView
is one click behindExpected behavior
slidesInView
in sync with what's actually in view.Thanks in advance