Jquery Auto expand Parent div when child div height is changed

自闭症网瘾萝莉.ら 提交于 2019-12-06 07:12:49

I'd track keyup events and watch the height of #child, for example:

var $child = $('#child');

var $container       = $('#container');
var container_height = $container.height();
$container.resizable();

$child.keyup(function() {
    var h = $child.height();
    if(h > container_height) {
        $container.height(h);
        container_height = h;
    }
});

Here's an updated version of your jsfiddle: http://jsfiddle.net/ambiguous/ULUJX/10/

If you want it to shrink as well then simply change the if and take care with the initial sizes of things.

i was testing out this and it works good

(function heightFix($){
    var parentCon = $('#container'),
        child = $('#child').height();
    if(parentCon.height() < child){
       parentCon.height(child)
    }
    setTimeout(function(){
        heightFix($);
    },100)
}(jQuery));

you can change the speed to update the height faster but if your going to use it in a editable box it would be better to use a event trigger like

$('#child').live('keypress', function(){
    var parentCon = $('#container'),
        child = $(this).height();
    if(parentCon.height() < child){
       parentCon.height(child)
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!