jquery $(window).height() 返回文档高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12103208/
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
jquery $(window).height() is returning the document height
提问by Fraser
I'm sure there is a simple error I'm making, but I am simply alerting $(window).height()
and it returns the same value as $(document).height()
.
我确定我犯了一个简单的错误,但我只是发出警报$(window).height()
,它返回与$(document).height()
.
I am on a 13" MBA and my window height of my browsers when maximised between 780px - 820px (roughly) but each time it returns a window height identical to that of document height. In every case on the site I am working on it is over 1000px.
我在 13" MBA 上,当我的浏览器窗口高度在 780px - 820px(大致)之间最大化时,但每次它返回的窗口高度与文档高度相同。在我正在处理的网站上的每种情况下超过 1000 像素。
What is going on here?
这里发生了什么?
alert($(window).height());
alert($(document).height());
回答by Tom Hubbard
With no doctype
tag, Chrome reports the same value for both calls.
如果没有doctype
标记,Chrome 会为两次调用报告相同的值。
Adding a strict doctype like <!DOCTYPE html>
causes the values to work as advertised.
添加严格的 doctype like<!DOCTYPE html>
会导致值按宣传的那样工作。
The doctype
tag must be the veryfirst thing in your document. E.g., you can't have any text before it, even if it doesn't render anything.
该doctype
标签必须是非常文档中的第一件事情。例如,在它之前不能有任何文本,即使它不呈现任何内容。
回答by Eduardo
I had the same problem, and using this solved it.
我有同样的问题,使用它解决了它。
var w = window.innerWidth;
var h = window.innerHeight;
回答by Xmindz
I think your document must be having enough space in the window to display its contents. That means there is no need to scroll down to see any more part of the document. In that case, document height would be equal to the window height.
我认为您的文档在窗口中必须有足够的空间来显示其内容。这意味着无需向下滚动即可查看文档的更多部分。在这种情况下,文档高度将等于窗口高度。
回答by Solomon Closson
Here's a question and answer for this: Difference between screen.availHeight and window.height()
这是一个问答: screen.availHeight 和 window.height() 之间的区别
Has pics too, so you can actually see the differences. Hope this helps.
也有图片,所以你可以真正看到差异。希望这可以帮助。
Basically, $(window).height()
give you the maximum height inside of the browser window (viewport), and$(document).height()
gives you the height of the document inside of the browser. Most of the time, they will be exactly the same, even with scrollbars.
基本上,$(window).height()
为您提供浏览器窗口(视口)内的最大高度,并$(document).height()
为您提供浏览器内文档的高度。大多数情况下,它们将完全相同,即使使用滚动条也是如此。
回答by Gourav Yadav
Its really working if we use Doctype on our web page jquery(window) will return the viewport height else it will return the complete document height.
如果我们在我们的网页上使用 Doctype,它真的有效 jquery(window) 将返回视口高度,否则它将返回完整的文档高度。
Define the following tag on the top of your web page:
<!DOCTYPE html>
在网页顶部定义以下标签:
<!DOCTYPE html>