[Help]: How to grab the content in the active/current slide from a different component file? #847
-
SummaryHello 👋 I have a use case, whereby I am looping over an array of images, but I wanted to grab the currently visible image, to be used in a different component file. I want this to be reactive, and by that I mean that if a new slide with a new image enters into view, it should update where it is being referenced. My thinking was using Svelte stores for keeping track of the current image, but I'm not sure how to go about this. I appreciate the help. If applicable, which variants of Embla Carousel are relevant to this question?
Additional informationNo response CodeSandbox exampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @shyakadavis, Thank you for your question. For the Embla side of things you need to know what slides are in view, and that's easy: <script>
import emblaCarouselSvelte from 'embla-carousel-svelte'
let emblaApi
function setSlideInViewToStore(emblaApi) {
// Returns slide indexes in view in an array format --> Example [3]
// NOTE that slide index 3 actually means slide nr. 4 because slide 1 has index 0
const slidesInView = emblaApi.slidesInView()
}
function onInit(event) {
emblaApi = event.detail
emblaApi.on('slidesInView', setSlideInViewToStore) // Update store when slides enter/leave view
setSlideInViewToStore(emblaApi) // Set store on init
}
</script>
<div class="embla" use:emblaCarouselSvelte on:emblaInit="{onInit}">...</div> For the Svelte side of things, you can check how Best, |
Beta Was this translation helpful? Give feedback.
Hi @shyakadavis,
Thank you for your question. For the Embla side of things you need to know what slides are in view, and that's easy: