1

For some reason both document height and window height return the same value, so when subtracting window from document height it returns 0. Anyone know why this might be happening?

console.log($(window).height());

console.log($(document).height());

The above both return the document height

4

3 回答 3

11

您可能忘记了<!DOCTYPE html>页面开头的文档类型。

于 2013-11-07T02:41:40.550 回答
5

那是因为您的文档填充了视口(可用于显示文档的区域)。

文档中

$(window).height();   // returns height of browser viewport
$(document).height(); // returns height of HTML document

如果您的文档足够长以占用一页以上,则第二个值可能大于第一个值(至少如果文档位于 iframe 内,请查看此演示)。可能还有其他情况会产生差异,但我现在没有想到。

于 2012-08-23T18:59:41.873 回答
4

我有一个类似的问题,我能够解决。我找到

$(window).height(); 

在 FF 中返回页面的整个高度。最终我意识到我在页面的任何 html 之前输出了一个调试短语“test”。Doc 类型之前的 IE。

删除此“测试”文本后,视口高度将按预期返回。

于 2012-12-31T13:47:36.730 回答