Hello ?
I'm new to GSAP/Scrolltrigger and trying to wrap my head around things still. So I apologize if this is a glaringly obvious rookie mistake.
I'm trying to modify the Layered pinning from bottom basic example into a re-usable module (to understand how to use Scrolltrigger for more than just one-offs).
I have a "one-off" working example here:
https://codepen.io/willhenderson/pen/zYyWpge
gsap.registerPlugin(ScrollTrigger);
ScrollTrigger.defaults({ scroller: "#custom-scroll-container" });
gsap.to("[data-slide]:not(:last-child)", {
ease: "none",
scrollTrigger: {
end: "+=300%",
pin: true,
scrub: true,
start: "top top",
trigger: "[data-module-stackedslides]"
},
stagger: 0.5,
yPercent: -100
});
gsap.set("[data-slide]", {zIndex: (i, target, targets) => targets.length - i});
But when I try to apply this logic more dynamically (and to more than one element) things fall apart and I'm not sure what I'm doing wrong:
gsap.registerPlugin(ScrollTrigger);
ScrollTrigger.defaults({ scroller: "#custom-scroll-container" });
let stacked_slides_modules = document.querySelectorAll('[data-module-stackedslides]');
stacked_slides_modules.forEach((module) => {
let slides = module.querySelectorAll('[data-slide]:not(:last-child)');
let end_spacing = 100 * slides.length;
slides.forEach((slide) => {
gsap.to(slide, {
ease: "none",
scrollTrigger: {
end: "+=" + end_spacing + "%",
pin: true,
scrub: true,
start: "top top",
trigger: module
},
stagger: 0.5,
yPercent: -100
});
});
});
Note: I'm setting the z-index inline in the style attribute on the "dynamic" version just for expediency in setting up the test (as I hadn't figured out how to handle that in the forEach yet).
Thank you for any insight you can offer.