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

How to Instantly Scroll to the Top of a Page Using JavaScript?

Linda Hamilton
Linda HamiltonOriginal
2024-11-30 22:35:13193browse

How to Instantly Scroll to the Top of a Page Using JavaScript?

How to Scroll to the Top of a Page Instantaneously in JavaScript

In scenarios where instant scrolling is preferred over smooth animations, JavaScript offers a convenient solution. To scroll to the top of a page instantly, you can leverage the native window.scrollTo() method.

Passing zero values as arguments to this method, window.scrollTo(0, 0), instantly scrolls the page to the top left corner.

Parameters:

  • xCoord: The pixel coordinate along the horizontal axis.
  • yCoord: The pixel coordinate along the vertical axis.

In the context of scrolling to the top of the page, both xCoord and yCoord are set to zero, resulting in immediate scrolling to the page's origin.

Example Usage:

To implement this functionality, you can use the following code:

window.scrollTo(0, 0);

This simple line of code will scroll your page to the top instantaneously, without any animation effects.

The above is the detailed content of How to Instantly Scroll 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