I'm trying to get the current index of the slides form the scrollTrigger carousel animation.
useEffect(() => {
let sections = gsap.utils.toArray(".panel");
const pin = gsap.to(sections, {
xPercent: -100 * (sections.length - 1),
ease: "none",
duration: 2,
scrollTrigger: {
trigger: triggerRef.current,
pin: true,
scrub: true,
snap: 1 / (sections.length - 1),
end: () => "+=" + sectionRef.current.offsetWidth,
onUpdate: (scrollTrigger) => {
const scrollPosition = scrollTrigger.scrollLeft;
const containerWidth = sectionRef.current.offsetWidth;
const index = Math.round(scrollPosition / containerWidth);
console.log("Current Index:", index);
},
},
});
I'm trying with onUpdate property, But nothing works. I'm getting NaN. Any suggestion on this guys ?