Home >Web Front-end >JS Tutorial >How Can JavaScript Automatically Scroll to the Bottom of a Web Page?
Automatic Page Scrolling to Bottom
When navigating a webpage with multiple elements, it can be cumbersome to manually scroll down to view content. This becomes even more challenging when the desired element is located at the bottom of the page. Fortunately, there's a solution using JavaScript that automates this process.
Solution Using JavaScript
To scroll to a specific element at the bottom of the page, use the following code:
window.scrollTo(0, document.body.scrollHeight);
This line of code retrieves the scroll height of the document body and scrolls the page to that position. However, in cases where elements are nested within other elements, the document body may not scroll as expected.
Handling Nested Elements
In such scenarios, target the element that scrolls and use its scroll height instead:
nestedElement.scrollTo(0, nestedElement.scrollHeight);
Additional Resources
For further reference, consider the following resources:
The above is the detailed content of How Can JavaScript Automatically Scroll to the Bottom of a Web Page?. For more information, please follow other related articles on the PHP Chinese website!