To make a DOM element draggable, the jQuery UI draggable() method is used. Thus the element can be moved by clicking on it with the mouse. It can also be dragged anywhere within the viewport.
Syntax:
The draggable () method can be used in either of the two forms:
- $(selector, context).draggable (options) Method
- $(selector, context).draggable (“action”, params) Method
First Method:
To specify that an HTML element can be moved in an HTML page, the draggable (options) method where the behavior of the elements involved is specified by the options parameter.
Syntax:
$(selector, context).draggable(options);
To use multiple options at a time using a JavaScript object, commas can be used to separate them.
Syntax:
$(selector, context).draggable({option1: value1, option2: value2..... });
The options that can be used with this method are listed below:
Option | Uses |
addclasses | To prevent the UI-draggable class from being added in the list of selected DOM elements when its value is set to false which is true by default. |
appendto | To indicate the element in which the draggable helper should be appended to while dragging. The default value is “parent”. |
axis | To constrain dragging to either the horizontal (x) or vertical (y) axis. Possible values:”x”, “y”. |
cancel | To restrict dragging from starting on specified elements. The “input,textarea, button,select,option” is its default value. |
connecttosortable | To define a list whose elements are interchangeable. The element is a part of the list at the end of placement. The default value is “false”. |
containment | To constrain dragging to within the bounds of the specified element or region. The default value is “false”. |
cursor | To define the CSS property of the cursor when the element moves, thus representing the shape of the mouse pointer. The default value is “auto”. |
cursorat | To set the offset of the dragging helper relative to the mouse cursor. Hash can be given as Coordinates using a combination of one or two keys: { top, left, right, bottom }. The default value is “false”. |
delay | To indicate the delay in milliseconds. After that time the first movement of the mouse is taken into account and the displacement may begin. The default value is “0”. |
disabled | To disable the ability to move items when set to true. Until this function is enabled (using the draggable (“enable”) instruction), the items cannot be moved. The default value is “false”. |
distance | To specify the number of pixels that the mouse must be moved before the displacement is taken into account. The default value is “1”. |
grid | To snap the dragging helper to a grid, every x and y pixels. Here, the array must be of the form [ x, y ]. The default value is “false”. |
handle | To restrict dragging from starting unless the mousedown occurs on the specified element(s). The default value is “false”. |
helper | To allow for a helper element to be used for dragging display. The default value is “original”. |
iframefix | To restrict the iframes from capturing the mousemove events during a drag. The default value is “false”. |
opacity | To specify the opacity of the element moved when moving. The default value is “false”. |
refreshpositions | To calculate all droppable positions on every mouse move when set to true. The default value is “false”. |
revert | To determine whether the element is moved back to its original position at the end of the move. The default value is “false”. |
revertduration | To determine the duration of displacement (in milliseconds) after which the element returns to its original position (see options.revert). The default value is “500”. |
scope | To group sets of draggable and droppable items, in addition to droppable’s accept option. The default value is “default”. |
scroll | To scroll the display if the item is moved outside the viewable area of the window, when set to true. The default value is “true”. |
scrollsenstivity | To determine how many pixels the mouse must exit the window to cause scrolling of the display. The default value is “20”. |
scrollspeed | To define the scrolling speed of the display once scrolling begins. The default value is “20”. |
snap | To adjust the display of the item being moved on other elements (which are flown). The default value is “false”. |
snapmode | To define the way the adjustment should be made between the moved element and those indicated in options.snap. The default value is “both”. |
snaptolerance | To define the maximum number of pixels in the difference in position necessary to establish the adjustment. The default value is “20”. |
stack | To control the z-index of the set of elements that match the selector, always brings the currently dragged item to the front. In things like window managers, it is very useful. The default value is “false”. |
zindex | To specify the z-index for the helper while being dragged. The default value is “false”. |
Example 1: