I have a React page with buttons on top in the header.
What I want is to be able to make the page auto-scroll smoothly to a specefic location of the react page when clicking on one of the buttons in the header
I have a React page with buttons on top in the header.
What I want is to be able to make the page auto-scroll smoothly to a specefic location of the react page when clicking on one of the buttons in the header
I used <button onClick={()=> window.location.replace(‘/#info’)}>Info to get to ABOUT ME.
The result was that the page did go to the location, but it didn’t scroll or smooth-scroll to the location
2
Answers
You can use a combination of href and CSS to achieve this.
CSS
React
Should be enough. If you want to use JS, you can use scrollIntoView but since you are using React, it won’t be recommended.
Follow this thread for more information
You can achieve this by using HashRouter from react router dom along with tweenjs for animation, here is an example:
HashRouter v5 with tweenJs
);
In this example I’m using react router dom v5 , if you would prefer to use react router dom v6, have a look at this repository:
HashRouter v6 with tweenJs