如何计算 child 绝对定位的容器的高度?我们可以假设只有直接子级是绝对定位的。子级具有相同的类(没有 ID)并且被轮换,一些使用填充定位,如下所示:
<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>
(对于其他浏览器,转换标签看起来当然不同)
附言如果有必要, children 可能有身份证
[更新] 也许像这样的代码可以工作,但不能,因为在循环之后 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;
}
});
最佳答案
尝试解决方案。
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);
关于javascript - 计算具有绝对定位子项的容器的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13526185/