Reputation: 67
We are using react.
I want to move the scroll position to the top when the button is pressed.
How do I set the scroll position to the top?
import "./styles.css";
const App = () => {
const list = [1, 2, 3, 4, 5, 6, 7, 8, 9];
return (
<div className="App">
{list.map((list) => (
<div
style={{ height: "200px", background: "blue", marginBottom: "20px" }}
>
{list}
</div>
))}
<button>button</button>
</div>
);
};
export default App;
Upvotes: 0
Views: 441
Reputation: 646
You can add method like this,
const scrollToTop = ()=> {
window.scrollTo({
top: 0,
left: 0,
behavior: "smooth"
})
}
<button onClick={scrollToTop}>Scroll</button>
Upvotes: 6