Moving ScrollBar Vertical position by a specified number of pixels

Topics related to client-side programming language.
Post questions and answers about JavaScript, Ajax, or jQuery codes and scripts.
Marius
Posts: 65

Moving ScrollBar Vertical position by a specified number of pixels

Hi
How can I move the window scrollbar vertical position up or down by a specified number of pixels?
I'm using this function to get the scrollbar vertical position.

Code: Select all

// function to get scrollbar vertical position
function scrollY() {
  return window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
}

MarPlo
Hi
There are various ways to set tthe scrollbar position. For example, you can use the scrollTo(x, y) function, or scrollTop property for vertical position.
The easiest way to scroll by a specified amount of pixels is to use the scrollBy(x, y) function (X for horizontally, Y for vertically).
So, you not need to get the current scrollbar position.

Code: Select all

// moves up by 100 pixels
window.scrollBy(0, -100);
- Positive values will scroll to the right and down the page. Negative values will scroll to the left and up the page.

Similar Topics