如何在没有(负)滚动条的情况下获取屏幕宽度?
我有一个元素,需要它的宽度,没有(!)垂直滚动条。
Firebug 告诉我主体宽度是 1280px。
其中任何一个在 Firefox 中都可以正常工作:
console.log($('.element').outerWidth() );
console.log($('.element').outerWidth(true) );
$detour = $('.child-of-element').offsetParent();
console.log( $detour.innerWidth() );
它们都返回 1263px,这是我正在寻找的值。
然而,所有其他浏览器都给我1280px。
是否有一种跨浏览器的方法可以在没有(!)垂直滚动条的情况下获得全屏宽度?
I have an element and need it's width without(!) vertical scrollbar.
Firebug tells me body width is 1280px.
Either of these work fine in Firefox:
console.log($('.element').outerWidth() );
console.log($('.element').outerWidth(true) );
$detour = $('.child-of-element').offsetParent();
console.log( $detour.innerWidth() );
They all return 1263px, which is the value I'm looking for.
However all other browser give me 1280px.
Is there a cross browser way to get a fullscreen width without(!) vertical scrollbar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
.prop("clientWidth")
和.prop("scrollWidth")
JavaScript 中的
: PS: 请注意,要可靠地使用
scrollWidth
,您的元素不应水平溢出jsBin 演示
您还可以使用
.innerWidth()
但此仅适用于body
元素.prop("clientWidth")
and.prop("scrollWidth")
in JavaScript:
P.S: Note that to use
scrollWidth
reliably your element should not overflow horizontallyjsBin demo
You could also use
.innerWidth()
but this will work only on thebody
element您可以通过简单地编写来使用 vanilla javascript:
您还可以使用它来获取文档的宽度,如下所示:
来源: MDN
您还可以获取整个窗口的宽度,包括滚动条,如下所示:
但是,并非所有浏览器都支持这一点,因此作为后备,您可以想要使用
docWidth
如上,并添加 滚动条宽度。来源:MDN
You can use vanilla javascript by simply writing:
You could also use this to get the width of the document as follows:
Source: MDN
You can also get the width of the full window, including the scrollbar, as follows:
However this is not supported by all browsers, so as a fallback, you may want to use
docWidth
as above, and add on the scrollbar width.Source: MDN
在没有滚动条的情况下获取正确宽度和高度的最安全位置是从 HTML 元素。试试这个:
浏览器支持相当不错,IE 9 及更高版本都支持此功能。对于旧版 IE,请使用此处提到的众多后备方案之一。
The safest place to get the correct width and height without the scrollbars is from the HTML element. Try this:
Browser support is pretty decent, with IE 9 and up supporting this. For OLD IE, use one of the many fallbacks mentioned here.
以下是一些假设
$element
是jQuery
元素的示例:Here are some examples which assume
$element
is ajQuery
element:试试这个:
您可以使用此代码获取有或没有滚动条的屏幕宽度。
在这里,我更改了
body
的溢出值并获取有和没有滚动条的宽度。Try this :
You can get the screen width by with and without scroll bar by using this code.
Here, I have changed the overflow value of
body
and get the width with and without scrollbar.我遇到了类似的问题,并使用
width:100%;
为我解决了这个问题。在尝试回答这个问题并意识到的本质将使这些 javascript 测量工具在不使用某些复杂功能的情况下变得不准确后,我得出了这个解决方案。 100% 执行是在 iframe 中处理它的一种简单方法。我不知道你的问题,因为我不确定你正在操作哪些 HTML 元素。
I experienced a similar problem and doing
width:100%;
solved it for me. I came to this solution after trying an answer in this question and realizing that the very nature of an<iframe>
will make these javascript measurement tools inaccurate without using some complex function. Doing 100% is a simple way to take care of it in an iframe. I don't know about your issue since I'm not sure of what HTML elements you are manipulating.这些解决方案都不适合我,但是我能够通过获取宽度并减去 滚动条的宽度。我不确定它的跨浏览器兼容性如何。
None of these solutions worked for me, however I was able to fix it by taking the width and subtracting the width of the scroll bar. I'm not sure how cross-browser compatible this is.
我知道这是一个老问题,但建议的答案也不适合我。我在网上找到了以下代码,并在 Chrome、Firefox 和 Safari 中进行了测试,似乎运行良好。
if (window.matchMedia("(max-width: 1339px)").matches) { // do some if matches }
我想我会在这里提到它,以防它对其他人有帮助。
I know this is an old question, but the suggested answer didn't work for me either. I found the following code online and tested it in Chrome, Firefox and Safari where it seems to work fine.
if (window.matchMedia("(max-width: 1339px)").matches) { // do something if matches }
Thought I would mention it here in case it helps someone else.
这是我用的
Here is what I use