jQuery - 检测元素高度是否大于窗口高度并对其进行处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7259991/
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 - Detect if element height is bigger than window height and do something about it
提问by Joonas
The tittle really says it all.
标题真的说明了一切。
Basically i want to detect if this div
's height
is bigger than window
height
and do something about it..
基本上我想检测此div
的height
比我的大window
height
,做一些事情..
I have done this but i cant get it to work http://jsfiddle.net/dhkCa/3Why wont it work?
我已经这样做了,但我无法让它工作http://jsfiddle.net/dhkCa/3为什么它不起作用?
Edit: Fixed a little error in the css code. Jsfiddle link updated.
编辑:修复了 css 代码中的一个小错误。Jsfiddle 链接已更新。
回答by David says reinstate Monica
The document
's contains all the elements within itself, and its height is a sum of the heights of all those elements (all the display:block
elements anyway, plus margin and padding); therefore no contained element can be taller than the document itself. What you need to do is compare the window
's height, not the document's:
所述document
的包含在其自身内的所有元素,它的高度是所有这些元件的高度(所有的总和display:block
反正元素,以及余量和填充); 因此所包含的元素不能比文档本身高。您需要做的是比较window
的高度,而不是文档的高度:
var div = $("div").height();
var win = $(window).height();
if (div > win ) {
$("div").addClass('red');
}