javascript中的页面高度

时间:2009-07-18 14:23:40

标签: javascript jquery height

当页面大于屏幕时,我无法在javascript中获得页面的高度。

我认为这会让我达到正确的高度:

$(document).height();

但只能获得屏幕的高度,与:

相同
$('body').height();

同样如下:

document.offsetHeight;

由于某些原因,所有这些示例仅返回屏幕的高度。有人可以帮忙吗?

5 个答案:

答案 0 :(得分:22)

使用jQuery(您指定的),$(document).height()将完全返回您要求的内容。

澄清height()方法的用法:

$('.someElement').height(); // returns the calculated pixel height of the element(s)
$(window).height();         // returns height of browser viewport
$(document).height();       // returns height of HTML document

我怀疑,如果$(document).height()不适合你,那就错了。你可能是:

  1. 过早称呼它。比如,在DOM准备好之前
  2. 有一些未清除的浮动,不会导致某些块级元素扩展到它们的实际高度。因此弄乱了身高计算。
  3. 有一些关键的绝对定位。绝对定位的元素不会对其父元素的高度计算做出贡献。

答案 1 :(得分:0)

如果你使用jQuery很酷,那么获取正文或html高度怎么样?像:

var winHeight = $('body').height();

答案 2 :(得分:0)

我正在寻找这个并降落在这里。如果没有Jquery,你也可以使用普通的JS。以下是:

console.log(document.body.clientHeight,document.body.scrollHeight,document.body.offsetHeight)

请注意以下链接以说明要使用的内容: Understanding offsetWidth, clientWidth, scrollWidth and -Height, respectively

答案 3 :(得分:-1)

我认为你需要添加window.pageYOffset(页面滚动的数量,以像素为单位)。

http://www.quirksmode.org/dom/w3c_cssom.html

答案 4 :(得分:-1)

不确定JQuery。但是,此链接可能有助于您了解各种属性以及它们在不同浏览器中以不同模式运行的方式。

  

http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html

相关问题