Dreamer Posted October 1, 2020 Share Posted October 1, 2020 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 More sharing options...
ZachSaucier Posted October 1, 2020 Share Posted October 1, 2020 Hey Dreamer and welcome to the GreenSock forums. A few things going wrong here: 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. 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. 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 4 Link to comment Share on other sites More sharing options...
Dreamer Posted October 1, 2020 Author Share Posted October 1, 2020 ZachSaucier Thanks for support!) Link to comment Share on other sites More sharing options...
GreenSock Posted January 16, 2021 Share Posted January 16, 2021 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 🎉 9 Link to comment Share on other sites More sharing options...
Kholja Posted April 22, 2021 Share Posted April 22, 2021 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}); } 1 Link to comment Share on other sites More sharing options...
ThePixelPixie Posted April 21, 2022 Share Posted April 21, 2022 @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 More sharing options...
GreenSock Posted April 21, 2022 Share Posted April 21, 2022 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now