jQuery Resizable and Draggable Tutorial with Example

Have you ever tried doing some animation using plain Javascript or moving DIVs here and there or resizing them?! Well, you know then how much pain it is as not only you have to tackle the difficult part of animation but also making it cross browser compatible. Well you probably know why I am stretching up so hard is just to tell you how easy it is to use jQuery and do some really fantastic things without even bothering of knowing how it is done internally. jQuery comes with really useful UI library that can be incorporated to do such tricks. In this small note, I will show you how to do Resizing and Dragging of a DIV using jQuery UI. I will show you the demo of a DIV, which can be resized by pulling up the edges and also dragged here and there.

Step 1: Setting up jQuery UI and CSS

We need to install jQuery UI library first in order to do resizing/dragging. Add following lines in your HEAD of html document.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js"></script> <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/themes/base/jquery-ui.css"/>
Code language: HTML, XML (xml)
Note that we have imported jquery and jquery-ui library. Also, to use Resizable we must import jquery-ui.css file. This stylesheet will be used by resizable to add resize corner and other internal styles.

Step 2: Write jQuery to Resize and Drag

For the demo we will define a small DIV which we will make resizable and draggable.
<div id="resizeDiv"></div>
Code language: HTML, XML (xml)
Now just add following jQuery code in your document.
$('#resizeDiv') .draggable() .resizable();
Code language: JavaScript (javascript)
The beauty of jQuery is that we can simply chain the function calling. First we called .draggable() function that makes the DIV draggable and then we called resizable(). You may want to define callback functions on start/stop/resize events. To do so we simply define:
$('#resizeDiv') .resizable({ start: function(e, ui) { alert('resizing started'); }, resize: function(e, ui) { }, stop: function(e, ui) { alert('resizing stopped'); } });
Code language: JavaScript (javascript)
Note that the callback function gets two arguments. First is event object and next is ui. The ui object has the following fields:
  • ui.helper – a jQuery object containing the helper element
  • ui.originalPosition – {top, left} before resizing started
  • ui.originalSize – {width, height} before resizing started
  • ui.position – {top, left} current position
  • ui.size – {width, height} current size
For more information about jQuery Draggable (Drag and Drop) read this article. jQuery Draggable (Drag and Drop)

Online Demo

Following is the online demo of jQuery Resizable and Draggable. (Demo Link)
Get our Articles via Email. Enter your email address.

You may also like...

58 Comments

  1. awesome code…. :)

  2. Thanks Gaurav…

  3. Thanks, very useful tutorial.

  4. john says:

    This is indeed great stuff!

    One question: I am trying to add multiple divs on a page that can be dragged, dropped and resized, but it appears that only one div can be activated at a time? I am trying to reactivate a given div on a mouseover, but the resizing doesn’t seem to be kicking back in. Is there some way to un-initialize the non-active divs?

    Thanks!

  5. Nan Meng says:

    There is some bug within jQuery resizable.
    If you specify aspectRatio:true and resize the target, the target’s aspect ratio is not well respected. i.e. If you resize the target back and forth quickly for a few times, a 4×4 square div may become a 4:1 rectangular.

  6. thangam says:

    i have one question…..

    we are developing a website. we need to include a non interactive slider in that. how to design a slider using JQuery Javascript?

  7. Tanuj Dave says:

    this is really awesome tutorial

    • Pham Tri says:

      Ya, that’s right

      • pradeep kumar says:

        ya, very true.

  8. Santiago says:

    This was a great tutorial. I’m using it now at work, for a website I am building for my company.
    However, I’ve gotten stuck because the code doesn’t work on Internet Explorer (not even Version 9, which is the most advanced, and supposedly the least crappy version of Explorer).

    Does anyone have any idea how I can improve/edit/manage the code or the CSS/HTML so that I can do the same thing in Explorer? Any help is appreciated.
    Thanks!

  9. adhe says:

    cant resizeable :(

    help me

  10. ashein says:

    Nice and clean solution, I was experiencing problems with constraining both operations to a parent div – apparently I was too intrusive with changing coords in the intermediate divs…

    One note: I wasn’t able to access ui.size / ui.sizeOriginal from the stop event of a draggable (used a shared event handler for both drag and resize stop), so I resorted to $(event.target) and its height() / width()

  11. Ali says:

    When I Resize it, yhe width and Height are resized proportionally but I want to resize freely. How? Please help me.

  12. Awesome buddy – would like it to be drag-able inside the parent div’s bounds. Will try doing some experiments on this for sure. Thanks

  13. varunmaggo says:

    thanks jquery guy!

  14. shanu says:

    Thanks..

  15. Very handy for my latest project!

  16. shamim says:

    hi this is fine but i want resizable two option:
    parent div and children div
    i was try this but not work

    parent div resizeable and same to child div resizeable

    any one can help me please…..

  17. Manoj says:

    Try to put an image in that div.
    Even the image should automatically adjust itself when we re-size the div but its
    not working.
    Please help me out.

    • Raneez says:

      put 100% to both width and height….i think it solve your issue

  18. awadhesh verma says:

    Good one…bt i want to add resize functionality in bottom and right sides of div..
    Plz help..
    Thnx in advance..

  19. Dave says:

    Great Tutorial! Thanks!

    I found that this isn’t working on IE9, though. Is anyone still actively checking this blog? Have you run into the same issue?

    Thanks,

    Dave

  20. Srinivas says:

    Great tutorials, but it is not working in IE9

  21. Alex says:

    Hey, thank you for the nice example, but what can i do to block dragging if my component reached the begin or the end of the container? thank you

  22. Shankar Raj D says:

    Its a greate work awsom!!!! its helped me alot… thanks ..

  23. Madhu says:

    Hai was write the following code but it is not working plz tell me whats the reason. why it is not working.
    thanks in advance

    <html>
    	<script src="jqueryall/jquery-1.6.2.js"></script>
    	<script src="jqueryall/ui/jquery.ui.mouse.js"></script>
    	<script src="jqueryall/ui/jquery.ui.core.js"></script>
    	<script src="jqueryall/ui/jquery.ui.widget.js"></script>
    	<script src="jqueryall/ui/jquery.ui.draggable.js">
    	</script>
    	<script>
    		function fun(){
    			$("#div").draggable();
    		}
    	</script>
    	<body>
    		<div id="div" style="height:100;width:100;background-color:blue;"></div>
    		<button name="click" onclick="fun()">click</button>
    	</body>
    </html>
    

  24. ChrisN says:

    Thanks a ton! :)
    Based on your code I could finally get jQuery Resizable to work with the jQuery SVG plugin.
    Your truly are my saviour! :)

  25. Servesh says:

    It’s very useful script. gr8..

  26. dev says:

    its not working with image resize and dragging

  27. Ashish says:

    Hello Viral,

    I want to create a div in which I can write text using text box. If text is overflowing then user can resize the div as per his choice. I am not able to resize the div, can you tell me whr is the problem? Below is my code.

    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
    <style type="text/css">
    #containerBox
    {
    	height:500px;
    	width:500px;
    	border:medium solid;	
    }
    #quoteBox
    {
    	height:100px;
    	width:100px;
    	border:medium dotted;
    }
    </style>
    
    <script>
    	$(
    		function() 
    		{
    			$("#quoteBox").resizable();
    		}
    	);
    
    </script>
    
    </head>
    
    <body>
    
    <div id="containerBox">
    	<div id="quoteBox">
        </div>
    </div>
    </body>
    

  28. rani says:

    really helpful..thankss…

  29. madhu says:

    Really it is very useful me thanks a lot

  30. Vladimir says:

    Thank you a lot! Things works fine for me.

    But here is question – is there possibility to limit area where on page div will be dragged/resized?
    For example:

    <div id="resizeOnlyInThatDiv">
    <div id="resizeDiv"></div>
    </div>
    


    ?

  31. sami says:

    hi viral..

    nice and thanks

  32. Amit Gupta says:

    Very Novice.

  33. rakesh says:

    how can i drag and drop multipal list item at same time using Ctrl key
    you have any idea

  34. Andreas Koppa says:

    According to my dic a tutorial is:
    “… a method of transferring knowledge that may be used as a part of a learning process. More interactive and specific than a book or a lecture; a tutorial seeks to teach by example and supply all the information to complete a certain task.”

    So, this is not a tutorial. It is just a selfishly demonstration on how to use iframes.

    • FlexiBecky says:

      Dude, there’s no iframes. Be nice.

    • vishal says:

      wen is d last time u visited your doctor??

  35. developer says:

    I’m having trouble resizing an element down to say 1px, which seems the absolute minimum. Any others came across this using jQueryUI’s resize function?

  36. Hey, great service you’ve created!

    A small question which you might answer:
    Is it possible to keep the resizable feature if you retrieve dynamic data in AJAX?
    (Use a form and change the content dynamically through AJAX / PHP).

    Like this:
    You have a pre-defined content that can both move and resize – “Move Me! … Resize Me! …”
    Can you bring in fresh content so that the box will change to something else (text or images) and still retain both functions.?

    1. Move around
    2. Change dimension / scale???

    I hope you understand me – had to take advantage of Google Translate :-)

    Sincerely
    Vetle Haugen

  37. Satya says:

    Hi,

    How to fix draggable() issue inside a scaled down container in jquery

  38. Poonam says:

    Hi ,
    How Can I apply resize/drag in my code.
    After clicking the ROI/metric button you can draw image on the canvas by mouse events.
    I want to select an image after drawing and resize it.
    IS it possible with this plugin
    http://jsfiddle.net/AhdJr/58/

  39. karthikeyan says:

    hi,

    thanks for the code its really nice one. But when i refresh it go goes to its original format again. But i need some thing like if i move or re-size then it shouldnot go to original format again.. please help me..

  40. HZ says:

    Hello, where can I download the source code?

  41. hassan says:

    Thank you so much for this example code, it worked great in IE9 when even the official demo had bizarre behavior!

  42. adamschodowy says:

    great job, but i can’t get it to work properly with image instead od div – reizing works fine but dragging not, can someone point a solution? except placing an img into div – this dont work too as far as img covers div

  43. Arnab says:

    Perfect solution. Thank you so much!!!

  44. Pravin K. says:

    Thanks for your awesome tutorial.
    Hi I am currently working on image resize and drag functionality.But not able to apply both properties to this image.We use callback function for this.
    Your input is useful.

  45. Sarah Jay says:

    Really helpful article. I solve my problem.Thanks for share that with us.

  46. Ricki Heicklen says:

    This is incredible! Quick question —

    I have overflow:scroll on for a div (it has a lot of text and it won’t all fit in the amount of space I want it to), and I’m having difficulty getting the little drag diagonal arrow at the bottom to stick to the bottom of my current window instead of just at the bottom of the top part of the window. How would you recommend I go about getting the bottom-right-corner drag lines to stick there even when I scroll? (The box I’m currently typing in does this!)

  47. g3 says:

    i have a problem, i need dragg and resize but only into determinated space, for example if i dragg the div near to botton right corner and i resize, i need stop resize in botton left corner ; a solution for this?

  48. g3 says:

    bottom right corner *

  49. db says:

    Can this be made to drag and resize a .png image? I’ve been trying and can’t seem to get it right.

    • Pohit says:

      Where, I can download this source code.

      • Virat says:

        You needs to copy code.

  50. Jilli Rose says:

    This is a very good helpful article. I appreciate your effort.

Leave a Reply

Your email address will not be published. Required fields are marked *