Jump to content
Search Community

Change Div colors based on scroll GSAP

rohitmishra89 test
Moderator Tag

Recommended Posts

Hello folks,

I am very new to the gsap framework.
I am trying to achieve a UI requirement for my project where I want to lock the div to change the child div's background color till the div reaches center of the viewport.

I tried to achieve this but not sure how to start changing color of the first div after the scroll-lock starts and also to complete the color change of the 3rd div little before the scroll-lock ends.

 

See the Pen wvVgMKd by mishraji89 (@mishraji89) on CodePen

Link to comment
Share on other sites

Yeah that's a good approach, if it works it works.

 

Another option could be this:

const toggleClass = (e) => {
  const _d = tl.scrollTrigger.direction;
  if (_d > 0) {
    e.classList.add("active");
  } else {
    e.classList.remove("active");
  }
};

cards.forEach((card, i) => {
  tl.to(
    card,
    {
      backgroundColor: "red",
      duration: 1 // Add some duration to see the color change effect
    },
    i * (1 / totalCards)
  ).call(() => toggleClass(card), [], "<+=0.05"); // Adjust the timing for each card
});

Here is my demo updated:

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

 

But if your code works the way you intend, just keep it: "If is not broken, don't fix it"

 

Hopefully this helps

Happy Tweening!

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