jQuery UI Draggable

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:

<!DOCTYPE html>  
<head>  
  <link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
  <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  <style>  
  #draggable { width: 50px; height: 50px; padding: 1em; background:crimson; color: white;}  
  </style>  
  <script>  
  $(function() {  
    $( "#draggable" ).draggable();  
  });  
  </script>  
</head>  
<body>  
     <div id="draggable" class="ui-widget-content">  
      <h3>Drag Me!!</h3>  
     </div>  
</body>  
</html>

Output:

Explanation:

In the above example, we are displaying draggable functionality. Here, no parameters are passed to the draggable() method.

Example 2: Using disable, distance and delay:

<!DOCTYPE html>  
   <head>  
      <link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
      <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
      <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
   </head>  
   <body>  
      <div id="div1" style="border:solid 2px; background-color:crimson; text-align:center;">  
         <span>Not Movable!!</span><br /><br />  
      </div>  
      <div id="div2" style="border:solid 2px; background-color:white; text-align:center;">  
         <span>  
            Drag me for 30px.
         </span>  
         <br /><br />  
      </div>  
      <div id="div3" style="border:solid 2px; background-color:crimson; text-align:center;">  
         <span>  
            Drag after 300ms.   
         </span>  
         <br /><br />  
      </div>  
 <script>  
         $("#div1 span").draggable (  
            { disabled: true }  
         );  
         $("#div2 span").draggable (  
            { distance: 30 }  
         );  
         $("#div3 span").draggable (  
            { delay: 300 }  
         );  
 </script>  
   </body>  
</html>

Output 1:

Output 2:

Explanation:

In the above example, we are displaying the usage of the disabled, delay and distance. There are important options in the drag function of jQuery UI.

Example 3: Moving content by duplicating:

<!DOCTYPE html>  
<head>  
   <link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
   <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
   <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
</head>  
<body>  
   <div id="div1" style="border:solid 2px; background:crimson; color:white; height:25px; text-align:center;">  
      <span>Duplication is possible.</span>  
   </div>  
   <script>  
      $("#div1 span").draggable ({  
         helper : "clone"  
      });  
   </script>  
</body>  
</html>

Output:

Explanation:

In the above example, we are using the using option helper with a value clone to move an element which is a clone of the selected element.

Example 4: Getting the current option value:

<!DOCTYPE html>  
<head>  
   <link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
   <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
   <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
</head>  
<body>  
   <div id="div1" style="border:solid 2px; background:crimson; color:white; text-align:center; height:25px;">  
      <span>Click to see the cursor type!!</span>  
   </div>  
   <script>  
      $("#div1 span").draggable();  
 
      $("#div1 span").bind('click', function( event ){  
          var cursor = $( "#div1 span" ).draggable( "option", "cursor" );  
          var cursorAt = $( "#div1 span" ).draggable( "option", "cursorAt" );  
          alert("Cursor type - " + cursor + ", cursorAt - " + cursorAt);  
     });  
   </script>  
</body>  
</html>

Output 1:

Output 2:

Explanation:

In the above example, we are setting the value of the Cursor and the CursorAt option at the time of execution to get the value of any option at any time during the script execution.

Second Method:

To act like prevent displacement, the draggable (action, params) method is used. A string is used to specify the action parameter. Along with that, multiple params can also be passed based on the specified action.

Syntax:

$(selector, context).draggable ("action", [params]);

The actions that can be used with this method are listed below:

Action Uses
destroy() To eliminate the drag functionality completely, so that the elements are no longer movable. The element will thus be returned back to its pre-init state.
disable() To disable the drag functionality, so that the elements cannot be moved until the next call to the draggable(“enable”) method.
enable() To reactivate the drag management so that the elements can be moved again.
option(optionname) To retrieve the value currently associated with the specified optionname. The name of the option to get is specified by the parameter optionname which is of type string.
option() To retrieve an object containing key/value pairs representing the current draggable options hash.
option(optionname, value) To set the value of the draggable option associated with the specified optionname. The name of the option to set is specified by the parameter optionname and the value parameter specifies the value to set for the option.
option(options) To set one or more options for the draggable. The map of option-value pairs to set is specified by the options parameter.
widget() To retrieve a jQuery object containing the draggable element.

Example 5:

<!DOCTYPE html>  
   <head>  
      <link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
      <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
      <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
   </head>  
   <body>  
      <div id="div1" style="border:solid 5px black; background-color:white; color:black; text-align:center;">  
         <span>Dragging Disabled. Not Draggable!</span><br><br>  
      </div>  
      <div id="div2" style="border:solid 5px black;background-color:gray; color:white; text-align:center;">  
         <span>Dragging Enabled. Draggable!</span><br><br>  
      </div>  
      <script>  
         $("#div1 span").draggable ();  
         $("#div1 span").draggable ('disable');  
         $("#div2 span").draggable ();  
         $("#div2 span").draggable ('enable');  
      </script>  
   </body>  
</html>

Output 1:

Output 2:

Explanation:

In the above example, we are displaying the use and the behavior of the “disable” and “enable” action.

 

Please Share