To scroll to the bottom of a webpage using JavaScript, you can manipulate the scrollTop
property of the window
object or the scrollTo()
method. Here are two common methods to achieve this:
- Using the
scrollTop
property:
window.scrollTop = document.body.scrollHeight;
2. Using the scrollTo()
method:
window.scrollTo(0, document.body.scrollHeight);
Both of these methods will scroll the page to the bottom by setting the vertical scroll position to the height of the entire document.
Here’s a complete example using the scrollTo()
method:
// Scroll to the bottom of the page
function scrollToBottom() {
window.scrollTo(0, document.body.scrollHeight);
}
// Call the function to scroll to the bottom
scrollToBottom();
Make sure you call these functions after the page has finished loading or at a suitable point in your code where the document’s height has been properly calculated.