Search code examples
jquerycssjquery-uijquery-ui-draggablejquery-ui-resizable

JQueryUI - Resizable forcing my div to switch from Position:Relative to Absolute


I'm trying to create a sliding, resizable bar inside a container. For some reason, the resizable method is converting the div from Position:Relative to Position:Absolute. This is then visually moving the box, and ruining the scroll action of the containers' container.

JSFiddle Example

I have tried hooking into the Stop and Resize event and converting back to Position:Relative, but it's quite a hack, and the results are not perfect. It seems like this should just work?

JQuery:

$(document).ready(function () {
    $(".Timeline").draggable({
        axis: "x",
        containment: "parent"
    })
    .resizable({
        containment: "parent",
        handles: 'e, w'
    });
});

HTML:

<div style="width: 800px; overflow: auto;">
    <div id="TimelineCanvas">
        <div class="TimelineContainer">
            <div class="Timeline">
            </div>
        </div>
    </div>
</div>

Style:

#TimelineCanvas
{
    width: 2000px;
    height: 150px;
}
.TimelineContainer
{
    height: 75px;
    width: 100%;
    border: 1px solid black;
}
.Timeline
{
    position: relative;
    height: 75px;
    width: 75px;
    background-color: Gray;
    cursor: move;
}

My best attempt at a fix so far:

$(document).ready(function () {
    $(".Timeline").draggable({
        axis: "x",
        containment: "parent"
    })
    .resizable({
        containment: "parent",
        handles: 'e, w',
        resize: function (event, ui) {
            $(this).css("height", '');
        },
        stop: function (event, ui) {
            $(this).css("position", '');
            $(this).css("top", '');
            $(this).css("height", '');
        }
    });
});

Any help would be appreciated; thanks!


Solution

  • I couldn't find the original defect report for why it happened, but it's pretty clearly intentional, quite possibly browser-specific... A dirty solution may be the best you have.

    https://github.com/jquery/jquery-ui/blob/master/ui/jquery.ui.resizable.js#L242

      // bugfix for http://dev.jquery.com/ticket/1749
      if (el.is('.ui-draggable') || (/absolute/).test(el.css('position'))) {
          el.css({position: 'absolute',top: iniPos.top,left: iniPos.left});
      }