javascript - Adjust parent div height according to child absolute div -
well question sequel of previous question in answer partly.
i need calculate content div height , apply wrapper div on basis of textarea auto expanding.
now expanding main div working wrapper div height not reducing accordingly when remove content of textarea/content div. because default calculating height of content div not resetting it. below lines that
var divheight = $('.content').height(); $('.wrapper').css('min-height', divheight+40+'px'); how make auto resize height of wrapper div.
- one solution not use
position:absoluteelement.
so main problem .content set position:absolute.
due absolute positioning need retrieve changed height dynamically in order apply parent div.
- the other solution:
var $content = $('.content'), // cache selectors better performance $wrapper = $('.wrapper'), $textarea = $("textarea"); function setcontentheight() { $wrapper.css('min-height', $content.height() + 40 ); } $textarea.on("input change", function() { $(this).height(1).height( this.scrollheight ); // manipulate textarea height setcontentheight(); }).change(); // trigger // when need reflect .content height? setcontentheight(); // $(window).on("resize", setcontentheight); // , on win resize .wrapper{ background:#f90; position:relative; } .content{ background:#dcffc8; margin:10px; position:absolute; top:0; left:0; right:0; padding:10px; } textarea{ outline:none; resize: horizontal; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <div class="content"> <p> lorem ipsum... </p> <div> <textarea></textarea> </div> </div> </div>
Comments
Post a Comment