JavaScript Window scrollBy() method

The JavaScript Window scrollBy() method is used to scroll the current document by a specified amount. It takes two arguments: the number of pixels to scroll the document horizontally, and the number of pixels to scroll the document vertically. It does not return a value.

Here are some examples of how the scrollBy() method can be used in JavaScript code:

  1. To scroll the current document by a specific amount, you can use the following code:
window.scrollBy(100, 50);
  1. To scroll the current document to the top of the page, you can use the following code:
window.scrollBy(0, -window.pageYOffset);
  1. To scroll the current document to the bottom of the page, you can use the following code:
var height = document.body.scrollHeight;
window.scrollBy(0, height - window.pageYOffset);

Overall, the scrollBy() method provides a convenient way to scroll the current document by a specified amount. It can be used for tasks such as scrolling to the top or bottom of the page, or other operations that require modifying the scroll position of the document.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: