The jQuery UI Draggable plugin makes selected elements draggable by mouse.
Draggable elements gets a class of ui-draggable
. During drag the element also gets a class of ui-draggable-dragging
. If you want not just drag, but drag-and-drop, see the jQuery UI Droppable plugin, which provides a drop target for draggables.
All callbacks (start, stop, drag) receive two arguments: The original browser event and a prepared ui object, view below for a documentation of this object (if you name your second argument 'ui'):
$("#draggable").draggable();
<!DOCTYPE html> <html> <head> <link type="text/css" href="http://jqueryui.com/latest/themes/base/ui.all.css" rel="stylesheet" /> <script type="text/javascript" src="http://jqueryui.com/latest/jquery-1.3.2.js"></script> <script type="text/javascript" src="http://jqueryui.com/latest/ui/ui.core.js"></script> <script type="text/javascript" src="http://jqueryui.com/latest/ui/ui.draggable.js"></script> <style type="text/css"> #draggable { width: 100px; height: 70px; background: silver; } </style> <script type="text/javascript"> $(document).ready(function(){ $("#draggable").draggable(); }); </script> </head> <body style="font-size:62.5%;"> <div id="draggable">Drag me</div> </body> </html>
This event is triggered when dragging starts.
start
event as an init option.
$('.selector').draggable({
start: function(event, ui) { ... }
});
start
event by type: dragstart
.
$('.selector').bind('dragstart', function(event, ui) {
...
});
This event is triggered when the mouse is moved during the dragging.
drag
event as an init option.
$('.selector').draggable({
drag: function(event, ui) { ... }
});
drag
event by type: drag
.
$('.selector').bind('drag', function(event, ui) {
...
});
This event is triggered when dragging stops.
stop
event as an init option.
$('.selector').draggable({
stop: function(event, ui) { ... }
});
stop
event by type: dragstop
.
$('.selector').bind('dragstop', function(event, ui) {
...
});
Remove the draggable functionality completely. This will return the element back to its pre-init state.
Disable the draggable.
Enable the draggable.
Get or set any draggable option. If no value is specified, will act as a getter.
The jQuery UI Draggable plugin uses the jQuery UI CSS Framework to style its look and feel, including colors and background textures. We recommend using the ThemeRoller tool to create and download custom themes that are easy to build and maintain.
If a deeper level of customization is needed, there are widget-specific classes referenced within the ui.draggable.css stylesheet that can be modified. These classes are highlighed in bold below.
Note: This is a sample of markup generated by the draggable plugin, not markup you should use to create a draggable. The only markup needed for that is <div></div>.