Before reading this article, I highly recommend reading the following previous article:
jQuery draggable is a interaction type widget that allows elements to be moved using the mouse. Draggable supports a number of methods (destroy, disable, enable, option, widgets) and events (create, drag, start and stop).
Let us create a simple html page and create some draggable elements.
Firstly, create a div using the following html code.
Now we add some css property, so use the following css for elements.
At last we add the jQuery code to make draggable div element.
After adding all above code now a draggable div is ready and we can drag this div element any where in our web page.
Output:
Options:
We can set various types of options for a draggable element to performance optimization. Let us consider some options .
addClasses Option:
If addClasses is set to false then it will prevent “ui-draggable” class to be added. This may be desired as a performance optimization when we call hundred of elements.
Example:
If we set the value of addClasses option equal to “false” then we can’t drag that element further.
Let us create a simple html page and create some draggable elements.
Firstly, create a div using the following html code.
- <divid="drag">
- <spanid="span1">Draggable Element</span>
- <br/>
- <spanid="span2">Drag Me</span>
- </div>
- <style>
- #drag
- {
- background-color: coral;
- border-style: groove;
- border-color: chocolate;
- font-size: 20px;
- width: 230px;
- height: 100px;
- margin-left: 150px;
- margin-top: 200px;
- text-align: center;
- }
- #span1
- {
- font-size: 26px;
- color: darkolivegreen;
- }
- </style>
- <script>
- $(document).ready(function()
- {
- $("#drag").draggable();
- })
- </script>
Output:
Options:
We can set various types of options for a draggable element to performance optimization. Let us consider some options .
addClasses Option:
If addClasses is set to false then it will prevent “ui-draggable” class to be added. This may be desired as a performance optimization when we call hundred of elements.
Example:
- //Initializer
- $("#drag").draggable(
- {
- addClasses: false
- });
- //Setter
- $("#drag").draggable("option", "addClasses", false);
After initialization we can get or set the option.
//Getter
var addClass_ = $("#drag ").draggable("option", "addClasses");
axis Option:
The axis option is used to define the axis around which element will drag either the horizontal (x) or vertical (y) axis by passing the value of axis.
Example:
Now this code allow draggable element to move in y axis not in x axis.
cursor Option:
The cursor option is used to define the type of css cursor during dragging.
Example:
Above code show a crosshair cursor when we drag the div.
delay Option:
The delay option define the time milliseconds after mousedown until dragging should start. This option can be used to prevent unwanted drags when clicking an element.
Example:
In above code we define delay of 5 seconds so dragging will start after 5 second of mousedown.
disabled Option:
The disabled option disables the draggable if set to true.
Example:
distance Option:
The distance option define the distance in pixel after mousedown the mouse must move before dragging should start. This option can be used to prevent unwanted drags when clicking on an element.
Example:
According to above code dragging will start after 20 pixel traveled by mouse.
grid Option:
The grid option snaps the dragging helper to a grid in every x and y pixels. The grid is defined in form of array[ x, y ].
Example:
Suppose if current pixel position of draggable element is 500,700 and we drag the element in “x” direction then next position will be 600,700.
opacity Option:
The opacity option define the opacity of draggable element during the dragging.
Example:
According to above code, the opacity of draggable elemnt will be 0.35 at the time of dragging.
revert Option:
The revert option define that whether the element should revert to its start position when dragging stops. If set to true the element will always revert.
Example:
revertDuration Option:
The revertDuration define duration of the revert animation, in milliseconds. The revertDuration option ignored if revert defined to false.
Example:
In above code we define the value of revertDuration equal to 5 sec, so draggable element will take 5 second in revert process.
scroll option:
If scroll option is set to true, container auto-scrolls while dragging. If set to false, container will not provide the scroll. Default value is “true”.
Example:
Above code doesn’t provide a auto scroll if element try to cross the screen.
Methods:
destroy Method:
The destroy method removes the draggable functionality completely. This will return the element back to its pre initialized state.
Example:
$("#drag").draggable("destroy");
Disable Method:
The disable method disable the draggable.
Example:
$("#drag").draggable("disable");
enable Method:
This method enable the draggable.
Example:
$("#drag").draggable("enable");
option Method:
The option is used to set or get the values of currently associated option.
Example 1:
Option method to set the options values.
In above code we use option method as setter to set the values of options.
Example 2:
Option method to get the value of options. Option method return key/value pairs representing the current draggable options hash.
Output:

Events:
create(event ,ui):
The create event triggered when draggable is created. Where event is of type Event, and ui is of type Object.
Example:
drag( event, ui ):
The drag event triggered while the mouse is moved during the dragging. It triggered immediately before the current move happens. Where event is of type Event, and ui is of type object like helper, position, offset.
Example:
Output:

start( event , ui ):
The start event triggered while the mouse is moved during the dragging where event is of type Event, and ui is of type Object like helper, position, offset.
Example:
Output:

stop( event , ui ):
Stop event triggered when dragging stops where event is of type Event, and ui is of type Object like helper, position, offset.
Example:
Output:

Today we learned about the draggable element, in next article we will learn about droppable. Till then keep coding.
Thanks for reading the article.
//Getter
var addClass_ = $("#drag ").draggable("option", "addClasses");
axis Option:
The axis option is used to define the axis around which element will drag either the horizontal (x) or vertical (y) axis by passing the value of axis.
Example:
- //Initializer
- $("#drag").draggable(
- {
- addClasses: false
- });
- //Setter
- $("#drag").draggable("option", "addClasses", false);
- This code allow draggable element to move in x axis not in y axis.
- //Initializer
- $("#drag").draggable(
- {
- axis: "x"
- });
- //Setter
- $("#drag").draggable("option", "axis", "y");
cursor Option:
The cursor option is used to define the type of css cursor during dragging.
Example:
- //Initializer
- $("#drap").draggable(
- {
- cursor: "crosshair"
- });
delay Option:
The delay option define the time milliseconds after mousedown until dragging should start. This option can be used to prevent unwanted drags when clicking an element.
Example:
- $("#drag").draggable(
- {
- delay: 500
- });
disabled Option:
The disabled option disables the draggable if set to true.
Example:
- //Initializer
- $("#drag").draggable(
- {
- disabled: true
- });
The distance option define the distance in pixel after mousedown the mouse must move before dragging should start. This option can be used to prevent unwanted drags when clicking on an element.
Example:
- //Initializer
- $("#drag").draggable(
- {
- distance: 20
- });
grid Option:
The grid option snaps the dragging helper to a grid in every x and y pixels. The grid is defined in form of array[ x, y ].
Example:
- //Initializer
- $("#drag").draggable(
- {
- grid: [100, 100]
- });
opacity Option:
The opacity option define the opacity of draggable element during the dragging.
Example:
- //Initializer
- $("#drag").draggable(
- {
- opacity: 0.35
- });
revert Option:
The revert option define that whether the element should revert to its start position when dragging stops. If set to true the element will always revert.
Example:
- //Initializer
- $("#drag").draggable
- ({
- revert: true
- });
The revertDuration define duration of the revert animation, in milliseconds. The revertDuration option ignored if revert defined to false.
Example:
- //Initializer
- $("#drag").draggable
- {
- revert: true,
- revertDuration: 5000
- });
scroll option:
If scroll option is set to true, container auto-scrolls while dragging. If set to false, container will not provide the scroll. Default value is “true”.
Example:
- //Initializer
- $("#drag").draggable
- ({
- scroll: false
- });
Methods:
destroy Method:
The destroy method removes the draggable functionality completely. This will return the element back to its pre initialized state.
Example:
$("#drag").draggable("destroy");
Disable Method:
The disable method disable the draggable.
Example:
$("#drag").draggable("disable");
enable Method:
This method enable the draggable.
Example:
$("#drag").draggable("enable");
option Method:
The option is used to set or get the values of currently associated option.
Example 1:
Option method to set the options values.
- $(document).ready(function()
- {
- //Initializer
- $("#drag").draggable
- ({
- axis: "x",
- addClasses: true,
- cursor: "crosshair",
- delay: 200
- });
- //Setter
- $("#drag").draggable("option", "axis", "y");
- $("#drag").draggable("option", "addClasses", false);
- $("#drag").draggable("option", "cursor", "crosshair");
- $("#drag").draggable("option", "delay", 150);
Example 2:
Option method to get the value of options. Option method return key/value pairs representing the current draggable options hash.
- //Initializer
- $("#drag").draggable
- ({
- axis: "x",
- addClasses: true,
- cursor: "crosshair",
- delay: 200
- });
- //Getter
- var Obj = $("#drag").draggable("option")
- alert("Values of Axis is=" + Obj["axis"] + "\n" +
- "Values of addClasses is=" + Obj["addClasses"] + "\n" +
- "Values of cursor is=" + Obj["cursor"] + "\n" +
- "Values of delay is=" + Obj["delay"] + "\n");
- })

Events:
create(event ,ui):
The create event triggered when draggable is created. Where event is of type Event, and ui is of type Object.
Example:
- $("#drag").draggable
- ({
- create: function(event, ui)
- {
- alert("drag has been created")
- }
- });
- Bind an event listener to the dragcreate event:
- $("#drag").on("dragcreate", function(event, ui) {});
The drag event triggered while the mouse is moved during the dragging. It triggered immediately before the current move happens. Where event is of type Event, and ui is of type object like helper, position, offset.
Example:
- $("#drag").draggable
- ({
- drag: function(event, ui)
- {
- alert("Left Position is=" + ui.position.left + "\n" +
- "top Position is=" + ui.position.top + "\n" +
- "Left offset is=" + ui.position.left + "\n" +
- "top offset is=" + ui.position.top + "\n")
- }
- });

start( event , ui ):
The start event triggered while the mouse is moved during the dragging where event is of type Event, and ui is of type Object like helper, position, offset.
Example:
- $("#drag").draggable
- ({
- start: function(event, ui)
- {
- alert("Left Position is=" + ui.position.left + "\n" +
- "top Position is=" + ui.position.top + "\n" +
- "Left offset is=" + ui.position.left + "\n" +
- "top offset is=" + ui.position.top + "\n")
- }
- });

stop( event , ui ):
Stop event triggered when dragging stops where event is of type Event, and ui is of type Object like helper, position, offset.
Example:
- $("#drag").draggable
- ({
- stop: function(event, ui)
- {
- alert("Left Position is=" + ui.position.left + "\n" +
- "top Position is=" + ui.position.top + "\n" +
- "Left offset is=" + ui.position.left + "\n" +
- "top offset is=" + ui.position.top + "\n")
- }
- });

Today we learned about the draggable element, in next article we will learn about droppable. Till then keep coding.
Thanks for reading the article.

Santhakumar MunuswamyPosted Jan 5, 2016, 11:09 PM
Thanks for nice article
Muhammad Aqib ShehzadPosted Jan 5, 2016, 7:41 AM
Nice one
Gowtham KPosted Jan 4, 2016, 11:57 AM
Good Article, Thanks for sharing
Pankaj Kumar ChoudharyPosted Jan 4, 2016, 7:49 AM
Thanks Upendra Pratap Shahi and Happy New Year............
Pankaj Kumar ChoudharyPosted Jan 4, 2016, 7:48 AM
Thanks Raja T........
Ankit BansalPosted Jan 4, 2016, 6:21 AM
Nice one..
Upendra Pratap ShahiPosted Jan 4, 2016, 4:26 AM
after a long time...again a great article....thanks for sharing...
Raja TPosted Jan 4, 2016, 3:24 AM
Nice, thanks for sharing