Answer the question
In order to leave comments, you need to log in
How to make a button for smooth scrolling to the top of the page (React)?
Guys, a trivial question is how to make a smooth scroll button in React. I did something like this:
import React from 'react';
import './BtnScrollUp.scss'
function BtnScrollUp() {
const handlerScrollUp = () => {
if (document.body.scrollTop > 0 || document.documentElement.scrollTop > 0) {
window.scrollBy(0, -50);
setTimeout(handlerScrollUp, 10);
}
}
return (
<div className={'btn-scroll-up'} onClick={handlerScrollUp}>▲</div>
);
}
export default BtnScrollUp;
Answer the question
In order to leave comments, you need to log in
No recursive timeouts needed. Just one challenge.
window.scrollTo({
top: 0,
left: 0,
behavior: 'smooth',
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question