[Help]: Change main slide when thumbs autoscroll #727
-
SummaryHi! I'm using main and thumbs sliders. In thumbs slider use autoplay. How can I change active slide in main slider, when thumb slide was scrolled by autoplay? thx! 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.
Answered by
davidjerleke
Feb 1, 2024
Replies: 1 comment
-
Hi @coldnanee, Thanks for your question. That's possible to achieve. A simple implementation would be: thumbsCarouselApi.on('select', () => {
mainCarouselApi.scrollTo(thumbsCarouselApi.selectedScrollSnap())
}) However, you might not want to disturb a user if they're dragging the main carousel by switching slide. So we can check if the pointer is down on the main carousel and only advance it when the pointer isn't down like so: const mainCarouselPointerDown = useRef(false)
useEffect(() => {
function setPointerState(api, eventName) {
mainCarouselPointerDown.current = eventName === 'pointerDown'
}
mainCarouselApi
.on('pointerDown', setPointerState)
.on('pointerUp', setPointerState)
}, [mainCarouselApi])
useEffect(() => {
thumbsCarouselApi.on('select', () => {
if (!mainCarouselPointerDown.current) mainCarouselApi.scrollTo(thumbsCarouselApi.selectedScrollSnap())
})
}, [mainCarouselApi, thumbsCarouselApi]) I hope it helps. Best, |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
coldnanee
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @coldnanee,
Thanks for your question. That's possible to achieve. A simple implementation would be:
However, you might not want to disturb a user if they're dragging the main carousel by switching slide. So we can check if the pointer is down on the main carousel and only advance it when the pointer isn't down like so: