javascript Jquery $(window).width() 不包括滚动条宽度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16802036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:02:22  来源:igfitidea点击:

Jquery $(window).width() excluding scrollbar width?

javascripthtmlcss

提问by Mr. Lavalamp

I've got a page that sets a lot of css properties using javascript and it uses $(window).width() to determine the page width. Among other things, it sets some div widths to be the full width of the page using that method. A It's done this way because a lot of the other calculations are done in javascript.

我有一个使用 javascript 设置很多 css 属性的页面,它使用 $(window).width() 来确定页面宽度。除其他外,它使用该方法将一些 div 宽度设置为页面的全宽。A 这样做是因为很多其他计算都是在 javascript 中完成的。

Anyway, I'm switching to a different navigation style and I set the body's overflow-y to be hidden. Unfortunately, now that the scrollbar has disappeared I'm left with a lovely white bar at the side of my page (in every browser I've tried) that is exactly the width of the scroll bar. Without specifically calculating the width of the scroll bar, which can get messy, how can I just get the width of the full browser including the scroll bar?

无论如何,我正在切换到不同的导航样式,并将主体的溢出-y 设置为隐藏。不幸的是,现在滚动条消失了,我的页面一侧(在我尝试过的每个浏览器中)留下了一个可爱的白色条,它正好是滚动条的宽度。没有专门计算滚动条的宽度,这可能会变得混乱,我怎样才能获得包括滚动条在内的完整浏览器的宽度?

Thanks!

谢谢!

EDIT:It was an order of operations issue, I was being an idiot. Sorry!

编辑:这是一个操作顺序问题,我是个白痴。对不起!

回答by seanjacob

window.outerWidth

window.outerWidth

The outerWidth property sets or returns the outer width of a window, including all interface elements (like toolbars/scrollbars).

externalWidth 属性设置或返回窗口的外部宽度,包括所有界面元素(如工具栏/滚动条)。

http://www.w3schools.com/jsref/prop_win_outerheight.asp

http://www.w3schools.com/jsref/prop_win_outerheight.asp

回答by Antarr Byrd

use document instead

改用文档

$(document).width()