javascript - Calculating height of absolute positioned nested childs -
i've stuck @ problem
.autoheightparent { width: 500px; /* example */ position: relative; } .absoluteposchild { /* absolute required make pretty drag , drop transitions 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 nesting </div> <!-- !! nesting --> <div class="autoheightparent"> <div class="absoluteposchild" style="left: 0px; width: 50px;"> <div> don't know height </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"> need automatcally calculate height of box above make box right after it. can see hardcoded 170px height. </div>
how guys solve problems this?
i have tried solve javscript solution looks wrong.
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; /* example */ position: relative; } .absoluteposchild { /* absolute required make pretty drag , drop transitions 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 nesting </div> <!-- !! nesting --> <div class="autoheightparent"> <div class="absoluteposchild" style="left: 0px; width: 50px;"> <div> don't know height </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 height calculated </div>
it uses setinteval check if of nested children changed it's height , recalculate height of parent box. need find better solution because of performance , code beauty.
snippets simplified compared real recalculations.
i free use flexbox or tool can solve this
i've solved issue relative positioning.
for complex "absolute position" animations made function translates absolute relative.
for example
.autoheightparent { width: 500px; /* example */ position: relative; display: flex; } .absoluteposchild { /* relative position here must 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 nesting </div> <!-- !! nesting --> <div class="autoheightparent"> <div class="absoluteposchild"> <div> don't know height </div> </div> <div class="absoluteposchild"> <div> g.child 2 </div> </div> </div> </div> </div> </body> </html>
you see parent box automatically resizes fine. can use translate3d child , parent box keeps it's size , position.
Comments
Post a Comment