javascript jQuery(document).width() 不包括可视区域之外的宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3722886/
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-25 02:02:27  来源:igfitidea点击:

jQuery(document).width() doesn't include width outside of viewable area

javascriptjqueryhtml

提问by Tony_Henrich

jQuery(document).width()doesn't include the total width (viewable + outside of viewable when there's a horizontal bar). It equals jQuery(window).width(). I thought jQuery(window).width()is the viewable area width and jQuery(document).width()is the total width.

jQuery(document).width()不包括总宽度(当有水平条时可见+可见之外)。它等于jQuery(window).width()。我认为jQuery(window).width()是可视区域宽度,jQuery(document).width()是总宽度。

How do I get the total width or how do I get the width of the area outside of the viewable area using jQuery?

如何获得总宽度或如何使用 jQuery 获得可视区域外区域的宽度?

回答by Peter Ajtai

To get the width of the "invisible" portion, simply subtract the total document width from the visible window width:

要获得“不可见”部分的宽度,只需从可见窗口宽度中减去总文档宽度:

jQuery(document).width() - jQuery(window).width()

jsFiddle example

jsFiddle 示例

alt text

替代文字



Like you write, $(document).width()is the total width, and $(window).width()is the width that is currently visible.

就像你写的那样,$(document).width()是总宽度,$(window).width()是当前可见的宽度。

Tested in the latest Chrome, Firefox, Internet Explorer, and Safari.

在最新的 Chrome、Firefox、Internet Explorer 和 Safari 中测试。

回答by Stephan Muller

I don't know if there's a jQuery equivalent, but

我不知道是否有等效的 jQuery,但是

document.getElementsByTagName('body')[0].offsetWidth

should give the correct width of the page, regardless of window size.

无论窗口大小如何,都应该给出正确的页面宽度。

回答by Tony_Henrich

It's my mistake. I was displaying the width and then was code somewhere after that that was adjusting the width of some elements, which caused the document to be wider.

是我的错。我正在显示宽度,然后在某处编写代码,然后调整某些元素的宽度,这导致文档变宽。

回答by SMHMayboudi

I think this codes are useful as well:

我认为这些代码也很有用:

self.innerWidth && (document.documentElement && document.documentElement.clientWidth) && document.body.clientWidth;