Jump to content
GreenSock

Dreamer

How to create anchor menu to horizontal scrolling page?

Recommended Posts

Hi, how can i create anchor links for a horizontal scrolling?

My version is not working correctly: 

$(".menu-item").on("click", function (event) {
    event.preventDefault();
    var id = $(this).attr('href');
    var left = $(id).offset().left;
    $('body,html').animate({
        scrollTop: left
    }, 1500);
});

 

See the Pen BaKgLvx by dreamerweb (@dreamerweb) on CodePen

Link to comment
Share on other sites

Hey Dreamer and welcome to the GreenSock forums. A few things going wrong here:

  1. Your ScrollTrigger's end is not what it should be. If you want the ScrollTrigger's duration to be 1 for 1 the duration of the horizontal section you should subtract out the width of the viewport from the end point.
  2. The offset that jQuery is giving you is not correct for the #contacts section. I don't know why but using the vanilla JS .offsetLeft works.
  3. I'd use GSAP's ScrollToPlugin because GSAP's performance is better than jQuery's. Also you have more flexibility using GSAP.

See the Pen YzqoNOK?editors=0010 by GreenSock (@GreenSock) on CodePen

  • Like 3
Link to comment
Share on other sites

  • 3 months later...

In case it's helpful to anyone else, I put together a demo that has directional snapping, variable width sections, and nav links: 

See the Pen xxEQNBB?editors=0010 by GreenSock (@GreenSock) on CodePen

 

🎉

  • Like 8
Link to comment
Share on other sites

  • 3 months later...

Maybe helpful in some cases, if you want to make the hashes work on first page impression ( i.e. calling https://mypage.com/#about ), you can get the hash from the url like this:
 

var hash = window.location.hash;
if(hash) {
  var left = $(hash)[0].offsetLeft;
  gsap.to("html", { scrollTo: left, duration: 1});
}

 

  • Thanks 1
Link to comment
Share on other sites

  • 11 months later...

@GreenSock - That demo above is very close to what I was just searching for. I'm curious about 2 things though:

 

1. Why does it automatically land on the 2nd panel instead of the first? I was trying to look through the code and wasn't seeing anything immediately that helped me understand that.

 

2. How would you move the nav from being static at the top to being static on the left side?

 

3. If each panel is slightly less than 100vw can the next panel be clicked to advance as well?

 

Thank in advance.

Link to comment
Share on other sites

1 hour ago, ThePixelPixie said:

1. Why does it automatically land on the 2nd panel instead of the first? I was trying to look through the code and wasn't seeing anything immediately that helped me understand that.

 

I adjusted the demo now to skip that. 

 

1 hour ago, ThePixelPixie said:

2. How would you move the nav from being static at the top to being static on the left side?

 

Just tweak the CSS to put it wherever you want.

 

1 hour ago, ThePixelPixie said:

3. If each panel is slightly less than 100vw can the next panel be clicked to advance as well?

 

Sure, you could add a click event listener to do that or put a transparent <div> covering the right edge, for example, so when people click on that it goes to the next section. You'd need to set up variables to track the current section, etc. (beyond the scope of these forums to do all that for you, but hopefully this gets you going in the right direction). 

 

Have fun!

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