如何确保网页打开时滚动条位于中间
我正在尝试创建一个水平滚动页面,并且希望页面打开时中间有滚动条,以便用户可以选择向左和向右滚动(默认情况下,它打开时左侧有滚动条)。我怎样才能做到这一点?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
评论后更新如果正文的宽度不超过浏览器的视口:
在元素内滚动:
Use this code to vertically & horizontally center a page (see also: this answer):
要仅水平居中,请使用:
Updated after comment If the body's width doesn't exceed the browser's viewport:
Scrolling inside an element:
Use this code to vertically & horizontally center a page (see also: this answer):
To only center horizontally, use:
在
onload< 中使用 JavaScript 函数
scrollTo
/代码> 事件。有关如何获取当前窗口大小的更多信息,请查看本文。Use the JavaScript function
scrollTo
in theonload
event. For more information on how to get the current window size, check out this article.一种可能性是在加载时使用 javascript
scrollto
页面(只需将其放入您的 onload 或 domready-event):只需将 x/y 值设置为您需要的值即可。
one possibility is to use javascripts
scrollto
when loading the page (simply put this in your onload or domready-event):just set the x/y values to the ones you need.
另一种方法是#href。如果您没有将尾部标记添加到网址的问题
another way is # href. If you don't have Problem with tail tag added to url