Jump to content
Search Community

Switching Between Dataset Attribute using ScrollTrigger

SB411

Go to solution Solved by SB411,

Recommended Posts

Posted

Hello GSAP masters,

 

I'm trying to figure out having my navbar changing its theme when it reaches a specific section with [data-theme="dark"] applied. Right now, what I do is using ScrollTrigger which I demonstrated in my CodePen. But some problems that I encountered:

  1. When the navbar is changing color, it doesn't happen smoothly. It's laggy and the element is changing one by one as opposed to all at once
  2. It doesn't detect the other section that also has the same id applied
  3. I feel like the code is messy and could be better, but I'm a newbie at this so I can't figure it out

What I have in mind is when the navbar enters the section with [data-theme="dark"] applied, the navbar itself can automatically apply the [data-theme="dark"] as well. Is that possible? Or is there any better approach for this?

 

Thank you very much for your help!

See the Pen GRVvojz by Syahr-Banu (@Syahr-Banu) on CodePen.

Posted

Hi @Syahr Banu welcome to the forum!

 

There are a few things here that make things complicated. This is all cool and all, but when starting out I would keep things simple and just update values directly instead of trying to update CSS variables, it is all possible, but you need to learn to walk before you start running! 

 

Something technical, there can only ever be one id with that name on a page, so creating two sections that have the id #darkSection is confusing. I've converted them into classes. 

 

Your code needs to be scoped to the current .darkSection, so you'll need to create a loop for each section, so ScrollTrigger knows which section it needs to run its logic. That is also why you only see markers for the first section, currently it only creates a ScrollTrigger for the first section. 

 

Again updating CSS variables are an abstraction on an abstraction, which will make things complicated fast. I would just update the color directly in the tween. Currently you're animating between these two strings var(--swatch-blue), var(--theme-navbar) which are what they are just strings, so GSAP happily does it but you want it to morph between the two colours. This is probably technically possible, by using Javascript to get the contents of the CSS variable and then passing that to the tween value, but as you can imagine, this will get complicated. 

 

GSAP can update CSS variables, but the variables get defined on the root element of the HTML (eg the <html> tag), so as you can see in the below demo you can update them, but that updates them all on the page. 

 

I've also placed some comments in your JS to explain certain things, be sure to read through them.

 

Hope it helps and happy tweening! 

 

See the Pen NWQvdyE?editors=1010 by mvaneijgen (@mvaneijgen) on CodePen.

 

Here the same demo, but with updating the values directly in the tween 

 

See the Pen zYgdNJq?editors=0010 by mvaneijgen (@mvaneijgen) on CodePen.

  • Like 1
  • Thanks 1
Posted

Hi @mvaneijgen! Thank you very much for your help. After studying your reply and looking at some other posts, I've finally found the solution I'm looking for 😄

 

Please let me know if my newest approach is still not preferable and could cause certain problems.

 

What I can't figure out now is how to make the transition for one color to another smooth like how you did it.

 

Appreciate your help thus far. Thanks!

 

See the Pen PoMKXWV by Syahr-Banu (@Syahr-Banu) on CodePen.

 

Posted

Hi,

 

@Carl made a great video showing a step by step process on how to achieve this:

 

  • Like 1
  • Thanks 1
Posted
2 minutes ago, Carl said:

Hey @Rodrigo thanks for posting the video!

Nah! Don't even mention it buddy.

 

Thanks for creating such amazing and helping content every week, it certainly makes our lives far easier here in the forums 😅

  • Like 1
  • Solution
Posted

Hi everyone! @mvaneijgen @Rodrigo @Carl

 

After studying all of your replies, I finally understand what I need to do according to my needs. So basically, what I did:

  1. Every time the dark section is in view, change the navbar color
    This is solved by looping through all section with data-theme = "dark" like how mvaneijgen suggested, & apply ScrollTrigger to each of them.

    Then, define the animation of the navbar by changing its data-theme attribute to dark as well.
     
  2. Make smooth transition for each color change
    After re-reading mvaneijgen's reply & looking at Carl's example, I came to understand that to achieve a smooth transition I need to update the tween values directly. I can't be using CSS variables nor can I change the data-themeattribute. The first causes laggy animation and the second just create an instant change.

    But in my case, updating the tween values directly will make the code super long and messy since my actual navbar will have a lot of elements. And on top of that, if I ever make any changes to the CSS variables, I'd need to keep track of all the related tween values.

    Therefore, after trying several approach & ended up asking ChatGPT 😁, I chose the 2nd approach (i.e. changing the data attribute) and the smooth transition is handled in the css through transition: all ease-in-out; property

    Thank you very much everyone for your help!

 

See the Pen NWQaEEB by Syahr-Banu (@Syahr-Banu) on CodePen.

  • Like 2

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.
×
×
  • Create New...