Home >Web Front-end >JS Tutorial >How Can I Instantly Jump to the Top of a Page Using JavaScript?

How Can I Instantly Jump to the Top of a Page Using JavaScript?

Linda Hamilton
Linda HamiltonOriginal
2024-11-30 22:37:12837browse

How Can I Instantly Jump to the Top of a Page Using JavaScript?

Achieve Instant Jump Scrolling to Page Top with JavaScript

If you're striving for an immediate jump to the top of the page when scrolling, without any graceful transitions, JavaScript offers a straightforward solution.

Method:

To instantly scroll to the top of the page using JavaScript, leverage the window.scrollTo() method. This method takes two parameters:

  • xCoord: The x-coordinate in pixels, which is typically 0 since scrolling to the top doesn't involve horizontal movement.
  • yCoord: The y-coordinate in pixels, which should be 0 to reach the page's top.

Example:

The following JavaScript code will instantly scroll the page to the top:

window.scrollTo(0, 0);

This code will skip the smooth scrolling animation and jump to the top of the page immediately. It's a simple yet effective way to implement instant top-of-page scrolling in your web project.

The above is the detailed content of How Can I Instantly Jump to the Top of a Page Using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn