How to adjust your scrollbar using javascript

How to adjust your scrollbar using javascript

You might be wonder when your customers ask you to design a page which will be opened at a certain position. Let's say, the page content should be in the middle of the browser viewport(in another way, the vertical scrollbar should be in the middle. Of course the content should be longer than the browser viewport height to get a vertical scrollbar.

How will we do it?Before that let us create some texts to make our content a little longer.To do so please click on the following link

Create text

Using scrollTop, you can change the scroll position to any appropriate value.

Try it here using document.body.scrollHeightGet me in the Middle
        //document.body.scrollHeight gives the appropriate position
        if(document.documentElement){
            document.documentElement.scrollTop =document.body.scrollHeight/4;
        }
        else if(document.body){
            document.body.scrollTop=document.body.scrollHeight/4;
        }
        
Try it here using document.documentElement.scrollHeightGet me in the Middle
        //or tested with
        //using document.documentElement.scrollHeight does not give the appropriate position.
        if(document.documentElement){
            document.documentElement.scrollTop =document.documentElement.scrollHeight/4;
        }
        else if(document.body){
            document.body.scrollTop=document.body.scrollHeight/4;
        }
    

document.body.scrollTop does not change the scroll position in IE7. Using document.documentElement.scrollTop gives the scroll position in IE7. However I have not tested it in any other browser.

No  Responses to "How to adjust your scrollbar using javascript"

Leave a Reply