How to update wrap content div when resizing?

795 Views Asked by At

I am using jQuery Ui to create a grid with drag and resize, the elements have different type of content, text and images. When I resize an element its height it is not update with the content height, eg. not recalculating the new image height for example.

HTML

<div class="column">
    <div class="portlet">
        <div class="portlet-header">Feeds</div>
        <div class="portlet-content">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div>
    </div>
    <div class="portlet">
        <div class="portlet-header">News</div>
        <div class="portlet-content">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div>
    </div>
</div>
<div class="column">
    <div class="portlet">
        <div class="portlet-header">Shopping</div>
        <div class="portlet-content">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div>
    </div>
</div>
<div class="column">
    <div class="portlet">
        <div class="portlet-header">Links</div>
        <div class="portlet-content"><img src="https://s-media-cache-ak0.pinimg.com/736x/12/64/da/1264da4a3f18207dc22592102abae40d--frangipani-tattoo-plumeria-flowers.jpg"></div>
    </div>
</div>

JS

$(function () {
    $(".column").sortable({
        connectWith: ".column",
        placeholder: 'ui-state-highlight',
        forcePlaceholderSize: true
    });

    $(".portlet").resize(function() {
        var myHeight = $(".portlet-content").scrollHeight;
        $(".portlet").outerHeight(myHeight);    
    });

    $(".portlet").resizable().addClass("ui-widget ui-widget-content ui-helper-clearfix ui-corner-all")
        .find(".portlet-header")
        .addClass("ui-widget-header ui-corner-all")
        .prepend("<span class='ui-icon ui-icon-minusthick'></span>")
        .end()
        .find(".portlet-content");
    $(".portlet-header .ui-icon").click(function () {
        $(this).toggleClass("ui-icon-minusthick").toggleClass("ui-icon-plusthick");
        $(this).parents(".portlet:first").find(".portlet-content").toggle();
    });
    $(".column").disableSelection();
});

JsFiddle here try resizing the box with the image and you will see it is not updating its wrapper height

1

There are 1 best solutions below

1
On

OK looks like I have resolved it by saying:

$(".portlet").resize(function() {
    $(".portlet").css("height", "auto");
});

And providing in the css:

.portlet-content {
    padding: 5px;
    height: 100%;
    float: left;
}

Working jsFiddle

not sure if it is the best way but works