The JavaScript Window pageYOffset property is a read-only property that returns the number of pixels that the current document has been scrolled vertically. It is commonly used to determine the vertical scroll position of the browser window.
Here are some examples of how the pageYOffset property can be used in JavaScript code:
- To retrieve the vertical scroll position of the current document, you can use the following code:
var y = window.pageYOffset;
// the value of y will be the number of pixels the document has been scrolled vertically
- To scroll the current document to a specific vertical position, you can use the following code:
window.scrollTo(window.pageXOffset, 500);
- To scroll the current document to the bottom of the page, you can use the following code:
// calculate the maximum vertical scroll position
var maxY = document.body.scrollHeight - window.innerHeight;
// scroll to the maximum vertical position
window.scrollTo(window.pageXOffset, maxY);
Overall, the pageYOffset property provides a convenient way to retrieve and manipulate the vertical scroll position of the current document. It can be used for tasks such as scrolling the document, animating elements, or other operations that require the vertical scroll position.
Leave a Reply