Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change scrollbar position?

Is it possible to change the scrollbar position when the user reaches a certain point scrolling down a web page? For example once you reached half way down down the page the scrollbar would move automatically back to the top.

like image 902
alan Avatar asked Aug 08 '09 03:08

alan


People also ask

How do I change the position of my scroll bar?

We can use the CSS “::-webkit-scrollbar” property which is responsible for changing the shape, color, size, shade, shadow, etc. of the scroll bar. But, here the property which we will use is the direction property of CSS for changing the position of the scroll bar.

How can I get the scrollbar position?

To get or set the scroll position of an element, you follow these steps: First, select the element using the selecting methods such as querySelector() . Second, access the scroll position of the element via the scrollLeft and scrollTop properties.

What is the position of scroll bar?

A standard scroll bar is located in the nonclient area of a window. It is created with the window and displayed when the window is displayed.

How do I move the scroll bar to the left CSS?

The way to do it is add div with direction: rtl; as parent element, and for child div set direction: ltr; . $( ". your selector of child element" ). wrap( "<div class='scroll'></div>" );


2 Answers

You can calculate the percentage of the current position of the scrollbar using the onscroll event, and if it reaches the 50 % the scroll position can be set to the top of the page with the scrollTo function:

window.onload = function () {    window.onscroll = function () {      var doc = document.body,      scrollPosition = doc.scrollTop,     pageSize = (doc.scrollHeight - doc.clientHeight),     percentageScrolled = Math.floor((scrollPosition / pageSize) * 100);        if (percentageScrolled >= 50){ // if the percentage is >= 50, scroll to top        window.scrollTo(0,0);       }     };  }; 

You can check my example here.

like image 108
Christian C. Salvadó Avatar answered Sep 16 '22 18:09

Christian C. Salvadó


Yup, I've seen it a few times. Here is some JS code:

window.scrollBy(0,50) 

50 is the amount of pixels you want to scroll by.

like image 24
thedayturns Avatar answered Sep 17 '22 18:09

thedayturns