Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make sure that a web-page opens up with scrollbar in the middle

I am trying to create a horizontally scrolling page and I want the page to open with scroll bar in the middle so that the user has option to scroll in both left and right directions(by default it opens with scrollbar at left). How can I do that?

like image 692
Jatin Avatar asked Nov 14 '22 15:11

Jatin


1 Answers

Updated after comment If the body's width doesn't exceed the browser's viewport:
Scrolling inside an 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 & horizontally center a page (see also: this answer):
 window.scrollTo(
     (document.body.offsetWidth -window.innerWidth )/2,
     (document.body.offsetHeight-window.innerHeight)/2
 );

To only center horizontally, use:

window.scrollTo((document.body.offsetWidth -window.innerWidth )/2, 0);
like image 130
Rob W Avatar answered Dec 15 '22 00:12

Rob W