Jump to content
Search Community

Using ToggleClass with multiple Nav's

MediaShmedia test
Moderator Tag

Go to solution Solved by Rodrigo,

Recommended Posts

Hi!

I feel like I'm missing the obvious logic piece here but it's escaping me... With the browser at 800px and up the active class gets applied as expected to the Desktop Nav, but only scrolling down. <800px, the class is applied to the Mobile Nav, but only when scrolling up?

 

Thank you so much, would appreciate any insight you could provide :)

See the Pen KKEvyNa by MediaShmedia (@MediaShmedia) on CodePen

Link to comment
Share on other sites

I'm a little confused about your code, you are using 2 navs you can use only 1 nav since both of them are the same thing nothing different,

 

or if you planning to use another nav for example with a different layout or something inside the mobile you can use matchMedia to add different animations for it 

Link to comment
Share on other sites

Is there anything else that I can add to help illustrate the issue - the ScrollTrigger is not applying the active class to the 2 nav menus as expected - it's only applied on scroll down on desktop, and scroll up on mobile...

Link to comment
Share on other sites

  • Solution

Hi,

 

If you run this in your code you'll see the issue:

let links = gsap.utils.toArray("nav a");
console.log(links); // array with 10 elements

Your links are 10 and the sections are just 5, so you're creating duplicated ScrollTrigger instances that toggle the class twice, see the problem?

 

This seems to work the way you intend:

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

 

Hopefully this helps

Happy Tweening!

  • Like 1
Link to comment
Share on other sites

Hi,

 

A little clarification regarding this:

13 hours ago, MediaShmedia said:

The CSS guy in me assumed display:none prevented render of one of the Triggers.

The triggers are not the links, the triggers are the panels. The point is that we get the id of each panel from the href attribute of each link:

let element = document.querySelector(a.getAttribute("href")),

That gets the attribute of the <a> tag which is a string: #one, then we look in the document for an element with that particular ID, which is the panel.

 

Finally display: none means that the element is not visible and removed from the document's flow, but is still in the DOM so it can still be selected.

 

Hopefully this clear things up.

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