Jump to content
Search Community

Scroll to section on button click

Yatsuba test
Moderator Tag

Recommended Posts

Hi,

 

I have a webflow website set-up which horizontally scrolls natively within webflow.

Now I want to let the user scroll to a certain section (ID section_cases) when they click on a button (ID scroll-button).

 

I've tried several set-ups but it doesn't seem to work. Here some code I tried.

Can someone point me in the right direction?

 

To view the webflow environment you can go here: https://preview.webflow.com/preview/portbase-dev-401cfc02012e4d1a6e442cbc33?utm_medium=preview_link&utm_source=designer&utm_content=portbase-dev-401cfc02012e4d1a6e442cbc33&preview=bd649b62c62c16a47c57d34f363f76d3&workflow=preview

 

<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.3/gsap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.3/ScrollTrigger.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.3/ScrollToPlugin.min.js"></script>

<script>
gsap.registerPlugin(ScrollToPlugin);

const section = document.getElementById('section_cases');
const timeline = gsap.timeline();
timeline.scrollTo(section, {
  duration: 1,
  ease: 'power2.inOut', 
});
const scrollButton = document.getElementById('scroll-button');
scrollButton.addEventListener('click', () => {
  timeline.play();
});
</script>

 

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/Nuxt/Gatsby or some other framework, you may find CodeSandbox easier to use. 

 

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 @Yatsuba welcome to the forum!

 

I would start by taking a look ad the scrollTo docs. The way you call gsap.scrollTo() is not how scrollTo works, you should call it like so ( https://greensock.com/docs/v3/Plugins/ScrollToPlugin)

gsap.to(window, {duration: 2, scrollTo: "#someID"});

This is just pseudo code, cause you didn't provide a minimal demo we could take a look at. If you're still struggling, please provide one there is no way for us to edit a live website. 

Link to comment
Share on other sites

I've created a button from your and thus removed the href. Having an href on a link will already navigate to the element, so if you want a link to do that you have to Event.preventDefault() on the click of the element otherwise the browser will take over (Read more here https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)

 

scrollTo will just accept an element, so if you don't want to do anything fancy you can just feed it the element. 

 

See the Pen GRGaxJQ?editors=1001 by mvaneijgen (@mvaneijgen) on CodePen

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