【发布时间】:2013-10-14 16:12:31
【问题描述】:
根据JQuery docsouterHeight(true) 函数返回元素的总高度,包括填充边框和边距。我遇到了这个承诺似乎被打破的案例。这是repro。
这里是html:
outer div height = <span id="div-target"></span><br/>
ul height (including margins) = <span id="ul-target"></span><br/><br/>
<div show-height-in='div-target'>
<ul show-height-in='ul-target'>here</ul>
</div>
和javascript:
var app = angular.module('application',[]);
app.directive('showHeightIn', function($log){
return function(scope, element,attrs) {
$('#' + attrs.showHeightIn).text($(element).outerHeight(true));
}
});
angular.bootstrap(document, ['application']);
在本例中,
- 元素使用添加 16px 顶部和底部边距的默认样式。这会将
- 的 outerHeight 设置为 52px。
现在父
元素仅显示 outerHeight(true) 20px - 不包括任何边距。
这个question on SO talks 是关于折叠边距的,我理解这个解释 - 在一定程度上。我的问题不是为什么不包括边距。
我的问题是如何确定呈现的 div 的高度。显然,在某些情况下,outerHeight(true) 是不正确的。
@Gaby:改变元素样式并不是唯一的方法。您还可以在前后添加 0 高度的块项目。但我不是在寻找一种方法来防止边距崩溃。我正在寻找一种通用方法来确定元素占据的总高度。我需要这个来避免限制我的 ng-scroll 指令的使用。
【问题讨论】: