2012-11-23 55 views
2

如何计算绝对定位儿童的容器高度?我们可以假设只有直接的孩子被绝对定位。孩子们有相同的类(无标识),并转动时,一些正在使用的填充定位,就像这样:计算绝对定位儿童的容器高度

<div class=my-view style="position: relative";> 
<div class=my-child style="position: absolute; top: 5px; left: 10px; -webkit-transform: rotate(13deg);"> 
<div class=my-child style="position: absolute; padding-bottom: 5%; padding-left: 10%; -webkit-transform: rotate(-6deg);"> 
</div> 
</div> 

(其他浏览器转换标记看起来当然是不同的)

附:如果有必要,孩子可能有身份证

[更新] 也许像这样的代码的东西会工作,但没有,因为在循环oTot.tot为零(“tot”就像“total”):

var oTot = { tot: 0 }; 
    $(".my-view .my-child").each(function() { 
    var h = $(this).outerHeight(); 
    var y = $(this).position().top + h; 
    if (y > oTot.tot) { 
     oTot.tot = y; 
    } 
    }); 

回答

0

尝试一下解决方案。

var oTot = { tot: 0 }; 
$(".my-child").each(function() { 
    var h = $(this).outerHeight(), 
     y = $(this).position().top + h; 

    if (y > oTot.tot) { 
     oTot.tot = y; 
    } 
}); 

console.log(oTot); 
+0

它给出的数字太大。这是因为容器的高度不等于它的孩子的高度的总和,因为孩子不是一个低于另一个,它们被绝对定位。我认为有必要检查函数内部子项的top + height值,并且如果它大于上一个循环步骤中的值,请增加最大值。我写了一些代码,但它不起作用,因为循环之后总数再次为零。我会把它放在我的帖子的末尾。 – camcam

+0

当调用callback $ .each时,您覆盖对象oTot。因此oTot变得不确定。看例子:http://jsfiddle.net/6cD94/1/ –

+0

是的,我昨天已经知道了。我想你的答案和我的更新算法一起给出了一幅图片,这取决于某人想要达到的效果。 – camcam