Hi guys, I know this question will sound trivial to you but I'm basically stuck here. I'm not able to attach a codepen but I'll try to make the question as easy as possible.
Basically, I have a foreach loop for pinning every section of my website, and that works very well. In addition to that I have 3 different timelines to change the body background (if you look at the example below)
let backgroundChange0 = gsap.timeline({paused:true});
backgroundChange0.to('body', {backgroundColor: '#fffce4'});
let backgroundChange1 = gsap.timeline({paused:true});
backgroundChange1.to('body', {backgroundColor: '#FFDD00'});
let backgroundChange2 = gsap.timeline({paused:true});
backgroundChange2.to('body', {backgroundColor: '#000'});
gsap.utils.toArray(".dds-panel").forEach((panel, i) => {
let mainTl = gsap.timeline({
scrollTrigger: {
trigger: panel,
onEnter: () => backgroundChange[i].play(),
onLeaveBack: () => backgroundChange[i].reverse(),
start: "center center",
pin: true,
pinSpacing: true,
toggleActions: "play none reverse none",
invalidateOnRefresh: true
}
});
}
I know this looks messy but I have to change the background color to different ones before pinning the section panel. The problem is that I can't make that backgroundChange[i] work inside onEnter, I even tried passing it as a string like ("backgroundChange" + i) but it doesn't work.
My goal is to use the forEach index to dinamically change to backgroundChange0, backgroundChange1, etc. inside the onenter and onleaveback event.
Thanks a lot for your help, let me know if it's clear, otherwise I will try to make a codepen with minimal coding.