我怎样才能确保一个网页打开与中间的滚动条 [英] How can I make sure that a web-page opens up with scrollbar in the middle

查看:88
本文介绍了我怎样才能确保一个网页打开与中间的滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个水平滚动页面,并且希望页面可以通过中间的滚动条打开,以便用户可以选择滚动左侧和正确的方向(默认情况下它打开左边的滚动条)。我该怎么做? 如果身体的宽度不超过浏览器的视口:
在元素中滚动

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



使用此代码垂直&水平居中页面(另请参阅:
$ b

  window.scrollTo(
(document.body / 7462737#7462737> this answer
):
.offsetWidth -window.innerWidth)/ 2,
(document.body.offsetHeight-window.innerHeight)/ 2
);

要仅横向居中,请使用:

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


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;


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);

这篇关于我怎样才能确保一个网页打开与中间的滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆