简体   繁体   中英

jQuery-ui draggable and resizable div can't resize from the left properly

I am trying to make a resizable and draggable div that is at a fixed position. The div looks fine when I resize it from the right or bottom; however, when I resize it from the left or top, the draggable div seems to increase height/weight in the opposite position.

Here is the code: HTML

<div id="draggableDiv" >
  <div id="resizableDiv">
    <div id="content"><div id="contentTitle"></div></div>
  </div>
</div>

JS:

 $('#draggableDiv').draggable({
    handle: '#contentTitle'
  });
  $("#resizableDiv").resizable({
    handles: 'n, e, s, w, ne, se, sw, nw'
  });

CSS:

  #draggableDiv {
     position:fixed !important;
     width:auto;
     height:auto;
     left:100px;
     top: 100px;
   }

   #resizableDiv {
     height: 200px;
     width: 200px;
   }

   #content { height:100%; width:100%; }

What should I do to make the draggable div resizes itself correctly?

You need to make the outer container #draggableDiv resizable. If you do that, then it is also required to modify your css a bit to set this div the desired dimensions. For example,

http://jsfiddle.net/RkNT4/

html

<div id="draggableDiv">
    <div id="resizableDiv">
        <div id="content">
            <div id="contentTitle">content title</div>
            conteeeeeent
        </div>
    </div>
</div>

js

$('#draggableDiv').draggable({
    handle: '#contentTitle'
});
$("#draggableDiv").resizable({
    handles: 'n, e, s, w, ne, se, sw, nw'
});

css

/*colors were added for the example to separate each div*/
    #draggableDiv {
        position:fixed !important;
        width:auto;
        height:auto;
        left:100px;
        top: 100px;
        background-color:green;
        height:200px;
        width:200px;
    }
    #contentTitle{
        background-color:red;
        cursor:move;
    }
    #resizableDiv {
        height: 100%;
        width: 100%;
    }
    #content {
        height:100%;
        width:100%;
        background-color:blue;
    }

You can apply the jquery draggable & resizable to the parent container, this works fine.

$('#draggableDiv').draggable({
   handle: '#contentTitle'
}).resizable({
   handles: 'n, e, s, w, ne, se, sw, nw'
});

BTW I did a Jsfiddle where you can check that
http://jsfiddle.net/kQtMF/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM