javascript $(document).height() 和 $(window).height() 有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14504195/
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
What is the difference between $(document).height() and $(window).height()
提问by epsilones
(Hope it is not a duplicate because I didn't find it when searching and googling)
(希望它不是重复的,因为我在搜索和谷歌搜索时没有找到它)
I am trying to find how to detect in some fixed-height div ('#div') when the scroll-bar is reaching the bottom of this latter div. Should I use $(document).height()and $(window).height()to detect this event ?
当滚动条到达后一个 div 的底部时,我试图找到如何在某个固定高度的 div ('#div') 中进行检测。我应该使用$(document).height()和$(window).height()来检测这个事件吗?
Edit : My div which is fixed-height and about which I set auto-scroll, so how to deal with that ? if I am suppose to use $('#div').height(), this height is fixed....
编辑:我的 div 是固定高度的,我设置了自动滚动,那么如何处理呢?如果我想使用 $('#div').height(),这个高度是固定的....
回答by Chase
In the .height()documentation:
在.height()文档中:
$(window).height(); // returns height of browser viewport
$(document).height(); // returns height of HTML document
In your case it sounds like you may want the height of the documentrather than the window. Think of it this way: The windowheight is what you see, but the documentheight includes everything below or above.
在您的情况下,听起来您可能想要 的高度document而不是window. 可以这样想:window高度就是您所看到的,但document高度包括下方或上方的所有内容。
EDIT:
编辑:
Checking for top and bottom on scroll with help from the scrollTop()method:
在scrollTop()方法的帮助下检查滚动的顶部和底部:
var bottom = $(document).height() - $(window).height();
$(document).scroll(function(){
var position = $(this).scrollTop();
if (position === bottom) {
console.log("bottom");
}else if(position === 0){
console.log("top");
} else {
console.log("scrolling");
}
});
回答by Westwick
The document height is the entire height of the whole document, even what is outside the viewable area. This could be thousands of pixels if you have a long page. The window height is just the viewable area.
文档高度是整个文档的整个高度,包括可视区域之外的高度。如果页面很长,这可能是数千个像素。窗口高度只是可视区域。
回答by Chirag Shah
Difference between $(window).height() and $(document).height() function.
$(window).height() 和 $(document).height() 函数的区别。
$(window).height() function:
$(window).height() 函数:
Ideally $(window).height() returns the pixel less height of browser window. This is always the height of current browser window. If you resize browser this value should change.
理想情况下 $(window).height() 返回浏览器窗口的像素高度。这始终是当前浏览器窗口的高度。如果你调整浏览器的大小,这个值应该会改变。
$(document).height() function: $(document).height() returns an unit-less pixel value of the height of the document being rendered.
$(document).height() 函数:$(document).height() 返回正在渲染的文档高度的无单位像素值。
In HTML if you dont declare DOCTYPE then all time HTML page returns $(window).height() and $(document).height() same value.
在 HTML 中,如果您不声明 DOCTYPE,那么所有时间 HTML 页面都会返回 $(window).height() 和 $(document).height() 相同的值。
<html>
<head>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.10.1.js'></script>
<script type='text/javascript'>
$(document).ready(function(){
$('#winheight').text($(window).height());
$('#docheight').text($(document).height());
});
</script>
</head>
<body>
<div id="console">
$(window).height() = <span id="winheight"></span> <br/>
$(document).height() = <span id="docheight"></span>
</div>
<p>Lorem ipsum dolor sit amet</p>
</body>
</html>
Output :
输出 :
$(window).height() = 750
$(document).height() = 750
Lorem ipsum dolor sit amet
After declare DOCTYPE its returns perfect value.
声明 DOCTYPE 后,它返回完美的值。
<!DOCTYPE HTML>
<html>
write above code here
</html>
Output :
输出 :
$(window).height() = 750
$(document).height() = 750
Lorem ipsum dolor sit amet
回答by syazdani
The height of the document is not necessarily the same as the height of the window. If you have a simple document with just a DIV and a little bit of text, the doc height will be miniscule compared to the height of the window.
文档的高度不一定与窗口的高度相同。如果您有一个只有一个 DIV 和一点文本的简单文档,与窗口的高度相比,文档高度将微不足道。
回答by Misha Reyzlin
Here's the code from jQuery source:
这是来自 jQuery 源代码的代码:
if (jQuery.isWindow(elem)) {
// As of 5/8/2012 this will yield incorrect results for Mobile Safari, but there
// isn't a whole lot we can do. See pull request at this URL for discussion:
// https://github.com/jquery/jquery/pull/764
return elem.document.documentElement["client" + name];
}
// Get document width or height
if (elem.nodeType === 9) {
doc = elem.documentElement;
// Either scroll[Width/Height] or offset[Width/Height] or client[Width/Height], whichever is greatest
// unfortunately, this causes bug #3838 in IE6/8 only, but there is currently no good, small way to fix it.
return Math.max(
elem.body["scroll" + name], doc["scroll" + name],
elem.body["offset" + name], doc["offset" + name],
doc["client" + name]);
}
So for $(window)clientHeightis used. Which, as @Drew correctly mentioned the height of visible screen area.
所以使用for $(window)clientHeight。其中,正如@Drew 正确提到的可见屏幕区域的高度。
For $(document)the whole scroll height of the current page will be used.
对于$(document)当前页面的整体滚动高度将被使用。

