Jump to content
Search Community

Change logo color over certain sections - Next JS

fantaz test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

Hi!

I'm a newbie using gsap and I would like to get some help regarding an issue mentioned in the post title.

I wanted to change the logo color when the user scrolls over a section which has class "dark-section", but I can't get it to work.

I've seen people using "gsap.utils.toArray('.classname')" method but here it does nothing. Instead, I'm using pure JS "Array.from" method. Does anyone know why this is happening? Scroll Trigger markers are being displayed correctly  so I guess that part works.

Here's a code sandbox link with my code.

Any help is appreciated ❤️

 

Link to comment
Share on other sites

Hi @fantaz and welcome to the GreenSock forums!

 

The link you're providing is to this thread so that takes us on an infinite loop! :D

 

I recommend you to fork one of our starter templates in Stackblitz in order to reproduce your issue:

https://stackblitz.com/edit/nextjs-5cm4qn

 

Also this could be related to proper cleanup. You should take a look at GSAP Context:

https://greensock.com/docs/v3/GSAP/gsap.context()

 

Also take a look at the resources in this page:

 

 

Hopefully this helps.

Happy Tweening!

Link to comment
Share on other sites

  • Solution

Hi,

 

Just use the callbacks ScrollTrigger has to offer:

https://greensock.com/docs/v3/Plugins/ScrollTrigger

 

This seems to work the way you intend:

useIsomorphicLayoutEffect(() => {
  let ctx = gsap.context(() => {
    const darkSections = document.querySelectorAll(".dark-section");
    const darkSectionsArr = Array.from(darkSections);

    /* let sections = gsap.utils.toArray(".dark-section"); */
    const logo = logoRef.current;
    const t = gsap.to(logo, {
      color: "#ffffff",
      paused: true,
    });
    darkSectionsArr.forEach((section) => {
      ScrollTrigger.create({
        trigger: section,
        markers: true,
        onEnter: () => t.play(),
        onEnterBack: () => t.play(),
        onLeave: () => t.reverse(),
        onLeaveBack: () => t.reverse(),
        start: "top 30",
        end: "bottom 50",
      });
    });
  }, headerRef);

  return () => ctx.revert();
}, []);

Hopefully this helps.

Happy Tweening!

  • Like 1
Link to comment
Share on other sites

Hi,

 

The reason the toArray method is not working because you have that inside a GSAP Context instance that has a scope defined:

let ctx = gsap.context(() => {
  const darkSections = document.querySelectorAll(".dark-section");
  const darkSectionsArr = Array.from(darkSections);

  let sections = gsap.utils.toArray(".dark-section");
  console.log(sections) // <- []
  console.log(darkSections) // <- NodeList[...]
}, headerRef);// <- this is your scope for GSAP Context

So the toArray method is looking for all the elements with the dark-section class inside that scope, not outside.

 

This works the way you intend:

// Doesn't take into account the scope now
let sections = gsap.utils.toArray(".dark-section");
let ctx = gsap.context(() => {
  const darkSections = document.querySelectorAll(".dark-section");
  const darkSectionsArr = Array.from(darkSections);

  console.log(sections)// <- [section.dark-section,...]
  console.log(darkSections)// <- NodeList[...]
}, headerRef);

Hopefully this clear things up.

Happy Tweening!

  • Like 1
Link to comment
Share on other sites

21 hours ago, Rodrigo said:

Hi,

 

The reason the toArray method is not working because you have that inside a GSAP Context instance that has a scope defined:

let ctx = gsap.context(() => {
  const darkSections = document.querySelectorAll(".dark-section");
  const darkSectionsArr = Array.from(darkSections);

  let sections = gsap.utils.toArray(".dark-section");
  console.log(sections) // <- []
  console.log(darkSections) // <- NodeList[...]
}, headerRef);// <- this is your scope for GSAP Context

So the toArray method is looking for all the elements with the dark-section class inside that scope, not outside.

 

This works the way you intend:

// Doesn't take into account the scope now
let sections = gsap.utils.toArray(".dark-section");
let ctx = gsap.context(() => {
  const darkSections = document.querySelectorAll(".dark-section");
  const darkSectionsArr = Array.from(darkSections);

  console.log(sections)// <- [section.dark-section,...]
  console.log(darkSections)// <- NodeList[...]
}, headerRef);

Hopefully this clear things up.

Happy Tweening!

this makes perfect sense! thank you so much.

Link to comment
Share on other sites

I've changed the code a bit to include changing hamburger menu background color when scrolling over dark sections, but it's not working.

What am I missing?

 

useIsomorphicLayoutEffect(() => {
    let ctx = gsap.context(() => {
      const logo = logoRef.current;
      const menu = menuRef.current;

      tl.current = gsap
        .timeline()
        .to(logo, {
          color: "#ffffff",
          paused: "true",
        })
        .to(menu.children, {
          backgroundColor: "#ffffff",
          paused: "true",
        });
      sections.forEach((section) => {
        ScrollTrigger.create({
          trigger: section,
          onEnter: () => tl.current.play(),
          onEnterBack: () => tl.current.play(),
          onLeave: () => tl.current.reverse(),
          onLeaveBack: () => tl.current.reverse(),
          start: "top 70",
          end: "bottom 85",
        });
      });
    }, headerRef);

    return () => ctx.revert();
  }, []);

 

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/Vue/Nuxt or some other framework, you may find StackBlitz easier to use. We have a series of collections with different templates for you to get started on these different frameworks: React/Next/Vue/Nuxt.

 

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,

 

This should work:

useIsomorphicLayoutEffect(() => {
  let sections = gsap.utils.toArray(".dark-section");
  let ctx = gsap.context(() => {
    const logo = logoRef.current;
    const menu = menuRef.current;

    tl.current = gsap
      .timeline({
      paused: true,
    })
      .to(logo, {
      color: "#ffffff",
    })
      .to(menu.children, {
      backgroundColor: "#ffffff",
    }, "<");
    sections.forEach((section) => {
      ScrollTrigger.create({
        trigger: section,
        onEnter: () => tl.current.play(),
        onEnterBack: () => tl.current.play(),
        onLeave: () => tl.current.reverse(),
        onLeaveBack: () => tl.current.reverse(),
        start: "top 70",
        end: "bottom 85",
        markers: true,
      });
    });
  }, headerRef);

  return () => ctx.revert();
}, []);

A couple of notes:

  • You had the gsap.utils.toArray() method outside the effect hook. That's not going to work because at that point the DOM is not rendered yet.
  • You had paused: true on each instance of the timeline. Just pause the timeline and not individual instances. While it's possible is far more difficult to manage correctly if you just want to play/reverse the entire timeline.

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...