How can I make sure the webpage opens with a scroll bar in the middle

I am trying to create a horizontal scroll page, and I want the page to open with the scroll bar in the middle so that the user can scroll left and right in both directions (by default this opens with a scroll bar on the left). How can i do this?

+5
source share
3 answers

Updated after comment. If the width of the body does not exceed the browser window:
Scroll inside the element :

var elem = document.getElementById("container"); //div#container
var elemWidth = elem.scrollWidth;
var elemVisibleWidth = elem.offsetWidth;
elem.scrollLeft = (elemWidth - elemVisibleWidth) / 2;


Use this code to vertically and horizontally center the page (see also this answer ):
 window.scrollTo(
     (document.body.offsetWidth -window.innerWidth )/2,
     (document.body.offsetHeight-window.innerHeight)/2
 );

To center only horizontally, use:

window.scrollTo((document.body.offsetWidth -window.innerWidth )/2, 0);
+1
source

Use the JavaScript function scrollToin the event onload. For more information on how to get the current window size, check out this article .

0
source

javascripts scrollto ( onload domready -event):

window.onload = function(){
  window.scrollTo(100, 100); // (X,Y)
}

x/y , .

0

All Articles