Jump to content
Search Community

Navigation links active in horizontal scroll - ScrollTrigger

dev33 test
Moderator Tag

Recommended Posts

Well no, it doesn't work entirely, It's not a full solution, It's a little breakdown of how that feature works and a bit of guidance into how you can acheive your goal.
 

Quote

I see it is necessary to add for all sections and I need it to be dynamic

You can loop round sections dynamically. You'd just need to write out a loop and grab information about the sections within that.

If you need any more help please show what you've tried.

  • Like 2
Link to comment
Share on other sites

const navbarLinks = document.querySelectorAll(".navbar a");
 
      function setActive(link) {
        navbarLinks.forEach(el => el.classList.remove("active"));
        link.classList.add("active");
      }
 
        navbarLinks.forEach(a => {
        let element = document.querySelector(a.getAttribute("href"));
            ScrollTrigger.create({
                trigger: element,
                start: "top top",
                end: "top bottom",
                onToggle: self => self.isActive && setActive(a)
            });
        });
Link to comment
Share on other sites

Just now, dev33 said:
const navbarLinks = document.querySelectorAll(".navbar a");
 
      function setActive(link) {
        navbarLinks.forEach(el => el.classList.remove("active"));
        link.classList.add("active");
      }
 
        navbarLinks.forEach(a => {
        let element = document.querySelector(a.getAttribute("href"));
            ScrollTrigger.create({
                trigger: element,
                start: "top top",
                end: "top bottom",
                onToggle: self => self.isActive && setActive(a)
            });
        });

I tried this but it doesn't work, something is missing

Link to comment
Share on other sites

Hi,

 

A few lines of code doesn't tell us exactly what could be wrong, but keep in mind that the setActive method you created is going to run twice. One for the ScrollTrigger instance leaving and another for the one entering.

 

Maybe this could be better:

elements.forEach((el) => {
  gsap.to(el, {
    scrollTrigger: {
      start: "top top",
      end: "bottom top",
      onToggle: () => el.classList.toggle("active"),
    },
  });
});

But again, without a minimal demo there is not much we can do.

 

Hopefully this helps.

Happy Tweening!

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

This is a little frustrating because I gave you the necessary information already.

I explained that containerAnimation allows you to fire off callbacks in a horizontal scrolling section.
I linked to the an article with demos, an explainer and a video demonstration.
I wrote you a small demo
And when you said that didn't work I mentioned that you could loop around dynamically and that it was just a guide and not a full solution.

Either way - here's a fully fleshed out solution. Please in future try to read the information and attempt it yourself, we're always happy to answer questions about it if you read through and something doesn't make sense.

If you're really stuck and need someone to write out a full solution or talk it through step by step with you, that's ok - we all get stuck, but maybe ask nicely? We're people, not robots. Just saying "but it doesn't work right" without any explanation, ignoring the advice given, posting screenshots of code without any explanation and repeating the title of the thread isn't really inspiring people to help. It all felt a bit demoralising to be honest. 

 

See the Pen PoxoZYa?editors=0011 by GreenSock (@GreenSock) on CodePen



I really hope this helps, but if you need more help, please try to be give us a bit more information and be less abrupt. Thanks.

  • Like 3
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...