Jump to content
GreenSock

akudlac

Change attribute of element after it goes through forEach array

Go to solution Solved by akapowl,

Recommended Posts

Hi,

 

In the attached CodePen, I'd like the initial state of the text on the right to be opacity 0.5. After an element is turned orange, I'd like its opacity to be 1, while those that haven't yet been orange staying at 0.5.

 

I've tried using gsap.set with the autoAlpha to .5 initially, while setting the element's CSS class to to 1, but it affects all the elements.

 

Thanks.

See the Pen 85fd1433b311d0197397addc90617a77 by akapowl (@akapowl) on CodePen

Link to comment
Share on other sites

 

Welcome to the forum @akudlac

 

Following the logic of that pen, in the forEach loop over the headlines, create a second ScrollTrigger and tl/tween setup, that is only tweening on the opacity of the element. That ScrollTrigger should have the same vars (start, end, etc.) as the first ScrollTrigger - but instead of the toggleActions, just set once: truethere. Then in your CSS set the opacity of .text to 0.5 and you should be good to go.

 

Give it a shot yourself and if you get stuck I'll be happy to help you figure out what went wrong with your approach.

 

  • Like 4
Link to comment
Share on other sites

On 11/1/2022 at 3:18 PM, akapowl said:

 

Welcome to the forum @akudlac

 

Following the logic of that pen, in the forEach loop over the headlines, create a second ScrollTrigger and tl/tween setup, that is only tweening on the opacity of the element. That ScrollTrigger should have the same vars (start, end, etc.) as the first ScrollTrigger - but instead of the toggleActions, just set once: truethere. Then in your CSS set the opacity of .text to 0.5 and you should be good to go.

 

Give it a shot yourself and if you get stuck I'll be happy to help you figure out what went wrong with your approach.

 

Hi,

 

I tried this. It works on the one element being affected in the loop. When it switches to the next element, it reverts to .5.

Link to comment
Share on other sites

It's pretty tough to troubleshoot without a minimal demo - the issue could be caused by CSS, markup, a third party library, your browser, an external script that's totally unrelated to GSAP, etc. Would you please provide a very simple CodePen or CodeSandbox that demonstrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best (avoid frameworks if possible). See if you can recreate the issue with as few dependancies as possible. If not, incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, then at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

Here's a starter CodePen that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

 

If you're using something like React/Next/Nuxt/Gatsby or some other framework, you may find CodeSandbox easier to use. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

Hi,

 

Hopefully I set this up correctly. I added the blue text color to the second trigger to show that it's firing.

 

Thanks.

 

See the Pen MWXezoV by akudlac (@akudlac) on CodePen

 

 

Link to comment
Share on other sites

On 11/3/2022 at 10:49 PM, akudlac said:

When it switches to the next element, it reverts to .5.

 

Looks like you are now also tweening on the autoAlpha on that other timeline's tween in the forEach loop that is targetting the texts - and I think that is the cause for the jump back.

 

Also in the second timeline's tween you created, you are tweening on opacity AND autoAlpha, which really isn't neccessary. autoAlpha is a handy shorthand combination of opacity with visibility being toggled, so better choose one of those - either opacity or autoAlpha.

 

I removed the autoAlpha from both timeline's tweens and got this (also got rid of the blue color). Is that what you were going for?

 

See the Pen BaVLPYz by akapowl (@akapowl) on CodePen

 

Link to comment
Share on other sites

2 hours ago, akapowl said:

 

Looks like you are now also tweening on the autoAlpha on that other timeline's tween in the forEach loop that is targetting the texts - and I think that is the cause for the jump back.

 

Also in the second timeline's tween you created, you are tweening on opacity AND autoAlpha, which really isn't neccessary. autoAlpha is a handy shorthand combination of opacity with visibility being toggled, so better choose one of those - either opacity or autoAlpha.

 

I removed the autoAlpha from both timeline's tweens and got this (also got rid of the blue color). Is that what you were going for?

 

 

 

 

Thank you. I knew something was occurring after but couldn't identify it.

 

Is there a way to remove the styling on the last one( Awesomeness)? 

 

Sorry for all the basic question, I appreciate the help.

Link to comment
Share on other sites

  • Solution
7 hours ago, akudlac said:

Is there a way to remove the styling on the last one( Awesomeness)? 

 

So it doesn't remain being orange when scrolling further? Sure.

 

If you don't mind the content on the left jumping back to the initial view too, the easiest way would be to just remove the "remaining" class on that content-div. What you could also do in that case is change the toggleActions to just "play reverse play reverse".

 

If you want to keep the content on the left at the last state when scrolling any further, with this setup you would probably have to split that first timeline/ScrollTrigger setup into two individual scroll-triggered timelines/tweens with the one for the content on the left having the exact same setup as the smallTimeline has now but an empty tween in place of the tween that changes the color now...

 

  contentTimeline 
    .to({},{ duration: 0.25 }, 0) 
    .set(relevantContent,{ autoAlpha: 1 }, 0.125)
  ;

 

...and the other one that is then only responsible for changing the headings having its toggleActions set to "play reverse play reverse".

 

See the Pen XWYXxOx by akapowl (@akapowl) on CodePen

 

 

 

Or alternatively just set up one ScrollTrigger for the headings with the toggleActions as mentioned above, and handle the logic for the visibility-change of the content on the left 'manually' in ScrollTrigger's callbacks, like so:

 

See the Pen rNKMgxO by akapowl (@akapowl) on CodePen

 

 

 

If you are fairly new to ScrollTrigger, this example in general might not be the best to get started with though, and I would strongly recommend reading up on how toggleActions and the callback-system work in the ScrollTrigger docs.

 

Nonetheless, I hope this will help a bit. Good luck woth the project!

 

  • Like 2
Link to comment
Share on other sites

6 hours ago, akapowl said:

 

So it doesn't remain being orange when scrolling further? Sure.

 

If you don't mind the content on the left jumping back to the initial view too, the easiest way would be to just remove the "remaining" class on that content-div. What you could also do in that case is change the toggleActions to just "play reverse play reverse".

 

If you want to keep the content on the left at the last state when scrolling any further, with this setup you would probably have to split that first timeline/ScrollTrigger setup into two individual scroll-triggered timelines/tweens with the one for the content on the left having the exact same setup as the smallTimeline has now but an empty tween in place of the tween that changes the color now...

 

  contentTimeline 
    .to({},{ duration: 0.25 }, 0) 
    .set(relevantContent,{ autoAlpha: 1 }, 0.125)
  ;

 

...and the other one that is then only responsible for changing the headings having its toggleActions set to "play reverse play reverse".

 

 

 

 

 

 

Or alternatively just set up one ScrollTrigger for the headings with the toggleActions as mentioned above, and handle the logic for the visibility-change of the content on the left 'manually' in ScrollTrigger's callbacks, like so:

 

 

 

 

 

 

If you are fairly new to ScrollTrigger, this example in general might not be the best to get started with though, and I would strongly recommend reading up on how toggleActions and the callback-system work in the ScrollTrigger docs.

 

Nonetheless, I hope this will help a bit. Good luck woth the project!

 

I am fairly new to it, and I will read up.

 

Thank you so much for your extensive help.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×