visit
In this article, you’ll learn how to use the scrollTo() method to set the scroll position to help users find what they’re looking for. You will also learn to scroll automatically or in response to click and submit events.
scrollTo()
methodscrollTo()
is one of many useful methods available on the window
interface. It allows you to scroll down (or across) by a specified number of pixels. For example, scroll 1000 pixels down.
The object can have three properties:
top
– specifies the number of pixels to scroll down.left
– specifies the number of pixels to scroll across.behavior
– specifies the scrolling animation.
An absolute majority of modern web applications are scrollable only vertically. For this reason, in practice, you will only use top
and behavior
properties.
You can use scrollTo()
method to scroll down in response to click, submit, and other events.
It’s also possible to use scrollTo()
in lifecycle methods (and useEffect() hook) to automatically set scroll position when the component mounts or re-renders.
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
<button
onClick={() => window.scrollTo({ top: 1000, behavior: "smooth" })}
>
Learn more
</button>
<div style={{ height: 1000 }}></div>
<p>
Lorem ipsum…
</p>
<div style={{ height: 1000 }}></div>
</div>
Alternatively, you can use lifecycle methods (or the useEffect() hook) to set the scroll position when the component mounts or every time it renders.
export default function App() {
useEffect(() => window.scrollTo({ top: 1000, behavior: "smooth" }), []);
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
Learn more
</button>
<div style={{ height: 1000 }}></div>
<p>
Lorem ipsum…
</p>
<div style={{ height: 1000 }}></div>
</div>
);
}
In plain JavaScript, you might use getElementById() to access DOM elements. Things work differently in React. You need to create a ref to store a reference to the DOM element.
To scroll the element into view, set top property to the number of pixels between the element and the top of the page. The value is stored in ref.current.offsetTop
property.
function App() {
const paragraphRef = useRef(null);
return (
<div className="App">
<button
onClick={() =>
window.scrollTo({
top: paragraphRef.current.offsetTop,
behavior: "smooth"
})
}
>
Learn more
</button>
<p ref={paragraphRef}>
Lorem Ipsum…
</p>
</div>
);
}
You can also use the scrollIntoView() method to scroll to an element in React.
There’s an important difference between the two methods. You need to call the scrollIntoView() method on the element itself.
function App() {
const paragraphRef = useRef(null);
return (
<div>
<button
onClick={() =>
paragraphRef.current.scrollIntoView({
behavior: "smooth",
block: "start"
})
}
>
Scroll to element
</button>
<p ref={paragraphRef}>
Lorem ipsum…
</p>
</div>
);
}
Hopefully, learning about scrollTo()
method and its many use cases can help you improve the user experience of your app. In this article, we also explored different ways to set a scroll position to a specific element in React.