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:
- To scroll the current document by a specific amount, you can use the following code:
window.scrollBy(100, 50);
- To scroll the current document to the top of the page, you can use the following code:
window.scrollBy(0, -window.pageYOffset);
- 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