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?
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;
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With