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"/>

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>

Now just add following jQuery code in your document.

$('#resizeDiv')
	.draggable()
	.resizable();

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');
		}
	});

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)



13 Comments

  • Gaurav - PHP/MySql programmer INDIA wrote on 25 January, 2010, 17:26

    awesome code…. :)

  • Viral Patel wrote on 25 January, 2010, 18:15

    Thanks Gaurav…

  • Can Aydoğan wrote on 27 January, 2010, 17:06

    Thanks, very useful tutorial.

  • john wrote on 9 March, 2010, 19:08

    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!

  • Nan Meng wrote on 7 May, 2010, 19:55

    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.

  • thangam wrote on 14 July, 2010, 11:44

    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?

  • Tanuj Dave wrote on 10 September, 2010, 16:42

    this is really awesome tutorial

  • Santiago wrote on 10 June, 2011, 22:20

    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!

  • adhe wrote on 13 June, 2011, 1:36

    cant resizeable :(

    help me

  • ashein wrote on 7 July, 2011, 1:35

    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()

  • Ali wrote on 3 November, 2011, 20:42

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

  • Susrut Mishra wrote on 25 January, 2012, 11:15

    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

Leave a Reply

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

*

Copyright © 2012 ViralPatel.net. All rights reserved.