最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Center the horizontal scroll bar of my site - Stack Overflow

programmeradmin3浏览0评论

I wonder if it is possible to center the horizontal scroll bar of my site using JavaScript or jQuery. A scroll bar appears for users who use resolutions like 1024 x 768, and therefore, would like her position always stay in the center.

Example:

.png

I wonder if it is possible to center the horizontal scroll bar of my site using JavaScript or jQuery. A scroll bar appears for users who use resolutions like 1024 x 768, and therefore, would like her position always stay in the center.

Example:

https://i.sstatic/YK8Xe.png

Share Improve this question asked Mar 6, 2014 at 16:37 user2297392user2297392 1
  • Even if you could, I don't think it would be good practice. If you don't want part of your site hidden for specific screen resolutions, you should make the site responsive. But if you really want to do this, you could add margin: 0 auto to center the page. – apohl Commented Mar 6, 2014 at 16:39
Add a ment  | 

1 Answer 1

Reset to default 10

Given that the scrollable element in question is document.body you could use the following JavaScript to centre the horizontal scrollbar:

document.body.scrollLeft = (document.body.scrollWidth - document.body.clientWidth) / 2
发布评论

评论列表(0)

  1. 暂无评论