Interactive online courses HTML Academy
2026-03-09 10:46 Diff

We told JavaScript to show the arrow button if it scrolls more than 200px. When you click this button, the page should scroll back to the beginning. This is not happening yet. How can we make the page scroll?

Use the scrollTo method:

window.scrollTo(X coordinate, Y coordinate)

The X coordinate indicates where we need to scroll the page to in the horizontal direction, and the Y coordinate specifies the value for the vertical direction. When the browser executes an instruction, the indicated point is displayed in the upper left corner of the window. The coordinates are entered in pixels, but there is no need to specify the units of measurement:

// Scrolls the page by 100px to the right and by 50px down window.scrollTo(100, 50);

If it is not possible to scroll the page to the specified coordinates, the browser will scroll the page as far as it can, but it will not enlarge the page. If the page is able to fit in the entire window and there is no scrollbar, then the browser will ignore this instruction.

There is no horizontal scroll bar on the news site that we are working on. Therefore, we will write 0 as the X coordinate, and we will only change the Y coordinate.

Let’s investigate how the scrollTo method works: Tell JavaScript to first scroll 400px down the page and then to scroll back up to the beginning.