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

How Can JavaScript Instantly Scroll to the Top of a Web Page?

Barbara Streisand
Release: 2024-11-30 09:27:11
Original
853 people have browsed it

How Can JavaScript Instantly Scroll to the Top of a Web Page?

Jump to the Page Top with JavaScript

Do you want to effortlessly scroll to the top of any web page, leaving the scrollbar at its peak? JavaScript makes this task a breeze.

One effective method is to utilize the native window.scrollTo() method. By specifying coordinates of 0 for both the horizontal and vertical axes, you can instantly transport the page to the top left corner without any smooth scrolling effects.

The syntax is straightforward:

window.scrollTo(xCoord, yCoord);
Copy after login

Where:

  • xCoord represents the pixel position on the horizontal axis (0 will take you to the leftmost point).
  • yCoord indicates the pixel position on the vertical axis (0 will scroll you to the top).

By setting both coordinates to 0, you can achieve the desired instant jump to the page top.

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

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template