-
Notifications
You must be signed in to change notification settings - Fork 292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not getting proper index for infinite=true #407
Comments
I'm wondering the same. I need to get the index of the current element after arrows are clicked with infinite & centerMode true. |
Hi @Richa-rani02, I was able to do a workaround when infinite & centerMode true. It's just a hunch, so I hope I'm not mistaken. If the carouse it's set to run ltr indexes will go increasily from 3 to the end.
Right side (arrow): Left side (arrow): I made this function that automatically does it for you (changeSlide): const responsive = {
superLargeDesktop: {
// the naming can be any, depends on you.
breakpoint: { max: 4000, min: 3000 },
items: 1
},
desktop: {
breakpoint: { max: 3000, min: 1024 },
items: 1
},
tablet: {
breakpoint: { max: 1024, min: 464 },
items: 1
},
mobile: {
breakpoint: { max: 464, min: 0 },
items: 1
}
};
const [activeIndex, setActiveIndex] = useState(0)
const images = [
{
src: slide1,
alt: "",
},
{
src: slide2,
alt: "",
},
{
src: slide3,
alt: "",
},
{
src: slide4,
alt: "",
},
]
const changeSlide = (previousSlide: number, currentSlide: number, dataSize: number) => {
let activeSlide = 0
// right arrow
if (previousSlide < currentSlide) activeSlide = currentSlide - 2 === dataSize ? 0 : currentSlide - 2
// left arrow
else activeSlide = currentSlide + (currentSlide <= dataSize && currentSlide >= 2 ? -2 : dataSize - 2);
setActiveIndex(activeSlide)
} As for the carousel I did this: <Carousel
infinite
autoPlay
centerMode
responsive={responsive}
className="w-full h-[70%] owl-carousel owl-theme text-center"
itemClass="px-1 flex justify-center"
keyBoardControl={true}
// changeSlide(previousSlide, currentSlide, dataSize = images.length)
afterChange={(previousSlide, { currentSlide }) => changeSlide(previousSlide, currentSlide, images.length)}
>
{images.map((img, i) => (
<div
key={`slide-${i}`}
className={`relative ${activeIndex !== i ? "w-[90%]" : "w-full"} h-full rounded-[10px] flex justify-center items-center bg-transparent shadow-[10px_40px_10px_5px_black]`}
>
{activeIndex !== i ?
<img
className='pointer-events-none object-contain rounded-[10px] opacity-10'
src={img.src}
alt={img.alt}
/>
:
<img
key={`slide-img-${activeIndex}`}
className='object-contain rounded-[10px]'
src={img.src}
alt={img.alt}
/>
}
</div>
)
)}
</Carousel> I hope this help you and others too! |
Can this get fixed upstream? |
@da8ah your solution worked perfectly for me! Thanks! |
DESCRIPTION
After change returns previousSlide and currentSlide which is working fine in case infinite=false
However, when infinite={true} is used, the current index doesn't seem correct
What i am trying to achieve:
want to track current index and next Index when arrow icon is clicked
Steps To Reproduce
https://codesandbox.io/s/multicarousal-demo-9vzw96?file=/src/App.js
Hosted Application
https://9vzw96.csb.app/
The text was updated successfully, but these errors were encountered: