计算绝对定位嵌套子节点的高度

时间:2016-07-28 11:45:13

标签: javascript html css flexbox css-hack

我坚持这个问题

.autoHeightParent {
  width: 500px; /* just for example */
  position: relative;
} 
.absolutePosChild {
  /* absolute is required to make pretty drag and drop transitions with interpolating left property */
  position: absolute; 
  display:flex;
  flex-direction: column;
  border: 1px solid green;
}
.absolutePosChild div {
  width: 100%;
}
<div class="autoHeightParent" style="height:170px;">
  <div class="absolutePosChild" style="left: 0px; width: 100px;">
    <div>
      Child content
    </div>
  </div>
  <div class="absolutePosChild" style="left: 100px; width: 150px;">
    <div>
      Child content with nesting
    </div>
    <!-- !! NESTING  -->
    <div class="autoHeightParent">
      <div class="absolutePosChild" style="left: 0px; width: 50px;">
        <div>
          We don't know this height too
        </div>
      </div>
      <div class="absolutePosChild" style="left: 50px; width: 100px;">
        <div>
          G.Child 2
        </div>
      </div>
    </div>
  </div>
</div>
<div style="border: 1px solid red">
I need to automatcally calculate height of the box above to make THIS box right after it. For now you can see hardcoded 170px height.
</div>

你们如何解决这样的问题?

我试图用javscript解决它但我的解决方案看起来不对。

function setRootHeight() {
  var root = document.getElementById("root");
  var allChilds = root.getElementsByTagName("*");
  var rootRect = root.getBoundingClientRect();
  var maxChildHeight = 0;
  _.each(allChilds, function(item) {
    var nodeRect = item.getBoundingClientRect();
    var nodeHeightRelativeToRoot = ( nodeRect.top + nodeRect.height ) - rootRect.top;
    if ( nodeHeightRelativeToRoot > maxChildHeight) maxChildHeight = nodeHeightRelativeToRoot;
  });
  root.style.height = maxChildHeight + "px";
}

setInterval(setRootHeight, 300);
.autoHeightParent {
  width: 500px; /* just for example */
  position: relative;
} 
.absolutePosChild {
  /* absolute is required to make pretty drag and drop transitions with interpolating left property */
  position: absolute; 
  display:flex;
  flex-direction: column;
  border: 1px solid green;
}
.absolutePosChild div {
  width: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.2.1/lodash.min.js"></script>
Test content Above
<div id="root" class="autoHeightParent">
  <div class="absolutePosChild" style="left: 0px; width: 100px;">
    <div>
      Child content
    </div>
  </div>
  <div class="absolutePosChild" style="left: 100px; width: 150px;">
    <div>
      Child content with nesting
    </div>
    <!-- !! NESTING  -->
    <div class="autoHeightParent">
      <div class="absolutePosChild" style="left: 0px; width: 50px;">
        <div>
          We don't know this height too
        </div>
      </div>
      <div class="absolutePosChild" style="left: 50px; width: 100px;">
        <div>
          G.Child 2
        </div>
      </div>
    </div>
  </div>
</div>
<div style="border: 1px solid red">
Visualization of that height was calculated
</div>

它使用setInteval检查嵌套子项是否有任何更改高度,然后重新计算父框的高度。实际上,由于性能和代码美感,我需要找到更好的解决方案。

与真实重新计算相比,摘录非常简单。

我可以自由使用flexbox或任何可以解决此问题的工具

1 个答案:

答案 0 :(得分:0)

我用相对定位解决了这个问题。

对于我复杂的“绝对位置”动画,我制作了将绝对翻译为相对的功能。

例如

.autoHeightParent {
  width: 500px; /* just for example */
  position: relative;
  display: flex;
} 
.absolutePosChild {
  /* relative position here must do absolutes' job here */
  position: relative; 
  display:flex;
  flex-direction: column;
  border: 1px solid green;
}
.absolutePosChild div {
  width: 100%;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <div class="autoHeightParent">
  <div class="absolutePosChild">
    <div>
      Child content
    </div>
  </div>
  <div class="absolutePosChild">
    <div>
      Child content with nesting
    </div>
    <!-- !! NESTING  -->
    <div class="autoHeightParent">
      <div class="absolutePosChild">
        <div>
          We don't know this height too
        </div>
      </div>
      <div class="absolutePosChild">
        <div>
          G.Child 2
        </div>
      </div>
    </div>
  </div>
</div>

</body>
</html>

您会看到父框自动调整大小。 但现在我可以将translate3d用于任何子节点和父节点,保持其大小和位置。