Friday, September 20, 2019

jQuery UI Draggable

jQuery UI draggable() method is used to make any DOM element draggable. Once the element is made draggable, you can move it by clicking on it with the mouse and drag it anywhere within the viewport.

Syntax:
You can use the draggable () method in two forms:

  1. 1.  $(selector, context).draggable (options) Method  
  1. 2.  $(selector, context).draggable ("action", params) Method   

First Method

The draggable (option) method specifies that an HTML element can be moved in the HTML page. Here, the option parameter specifies the behavior of the elements involved.
Syntax:
  1. $(selector, context).draggable(options);  
You can use one or more options at a time using JavaScript object. In the case of multiple options, you should use commas to separate them. For example:
  1. $(selector, context).draggable({option1: value1, option2: value2..... });   
Following is a list of different options that can be used with this method:
OptionDescription
addclassesIf this option is set to false, it will prevent the UI-draggable class from being added in the list of selected DOM elements. By default its value is true.
appendtoIt specifies the element in which the draggable helper should be appended to while dragging. By default its value is "parent".
axisThis option constrains dragging to either the horizontal (x) or vertical (y) axis. Its possible values are:"x", "y".
cancelThis option is used to prevent dragging from starting on specified elements. By default its value is "input,textarea, button,select,option".
connecttosortableThis option is used to specify a list whose elements are interchangeable. At the end of placement, the element is part of the list. By default its value is "false".
containmentConstrains dragging to within the bounds of the specified element or region. By default its value is "false".
cursorIt is used to specify the CSS property of the cursor when the element moves. It represents the shape of the mouse pointer. By default its value is "auto".
cursoratIt sets the offset of the dragging helper relative to the mouse cursor. Coordinates can be given as a hash using a combination of one or two keys: { top, left, right, bottom }. By default its value is "false".
delayIt specifies the delay in milliseconds, after which the first movement of the mouse is taken into account. The displacement may begin after that time. By default its value is "0".
disabledIt disables the ability to move items when set to true. Items cannot be moved until this function is enabled (using the draggable ("enable") instruction). By default its value is "false".
distanceThe number of pixels that the mouse must be moved before the displacement is taken into account. By default its value is "1".
gridIt snaps the dragging helper to a grid, every x and y pixels. The array must be of the form [ x, y ]. By default its value is "false".
handleIf specified, restricts dragging from starting unless the mousedown occurs on the specified element(s). By default its value is "false".
helperIt allows for a helper element to be used for dragging display. By default its value is "original".
iframefixIt prevents iframes from capturing the mousemove events during a drag. By default its value is "false".
opacityOpacity of the element moved when moving. By default its value is "false".
refreshpositionsIf set to true, all droppable positions are calculated on every mousemove. By default its value is "false".
revertIt indicates whether the element is moved back to its original position at the end of the move. By default its value is "false".
revertdurationIt indicates the duration of displacement (in milliseconds) after which the element returns to its original position (see options.revert). By default its value is "500".
scopeIt is used to group sets of draggable and droppable items, in addition to droppable's accept option. By default its value is "default".
scrollwhen set to true (the default), the display will scroll if the item is moved outside the viewable area of the window. by default its value is "true".
scrollsenstivityIt indicates how many pixels the mouse must exit the window to cause scrolling of the display. By default its value is "20".
scrollspeedIt indicates the scrolling speed of the display once scrolling begins. By default its value is "20".
snapIt adjusts the display of the item being moved on other elements (which are flown). By default its value is "false".
snapmodeIt specifies how the adjustment should be made between the moved element and those indicated in options.snap. By default its value is "both".
snaptoleranceIt specifies the maximum number of pixels in the difference in position necessary to establish the adjustment. By default its value is "20".
stackIt controls the z-index of the set of elements that match the selector, always brings the currently dragged item to the front. It is very useful in things like window managers. By default its value is "false".
zindexz-index for the helper while being dragged. By default its value is "false".

jQuery UI Draggable() method example 1

Let's take an example to demonstrate draggable functionality passing no parameters to the draggable() method.

  1. <!DOCTYPE html>  
  2. <head>  
  3.   <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  4.   <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  5.   <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  6.   <style>  
  7.   #draggable { width: 100px; height: 100px; padding: 0.5em; background:#7fffd4;}  
  8.   </style>  
  9.   <script>  
  10.   $(function() {  
  11.     $( "#draggable" ).draggable();  
  12.   });  
  13.   </script>  
  14. </head>  
  15. <body>  
  16.      <div id="draggable" class="ui-widget-content">  
  17.       <p>I am draggable!</p>  
  18.      </div>  
  19. </body>  
  20. </html>  

jQuery UI Draggable() method example 2

How to use disable, distance and delay:
Let's take an example to demonstrate the usage of three important options disabled, delay and distance in the drag function of jQuery UI.
  1. <!DOCTYPE html>  
  2.    <head>  
  3.       <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  4.       <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  5.       <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  6.    </head>  
  7.    <body>  
  8.       <div id="div1" style="border:solid 1px;background-color:pink;">  
  9.          <span>I am not movable.</span><br /><br />  
  10.       </div>  
  11.       <div id="div2" style="border:solid 1px;background-color:lightgreen;">  
  12.          <span>  
  13.             Dragging will start only after you drag me for 50px  
  14.          </span>  
  15.          <br /><br />  
  16.       </div>  
  17.       <div id="div3" style="border:solid 1px;background-color:lightyellow;">  
  18.          <span>  
  19.             Wait for 200ms before starting to drag.   
  20.          </span>  
  21.          <br /><br />  
  22.       </div>  
  23.  <script>  
  24.          $("#div1 span").draggable (  
  25.             { disabled: true }  
  26.          );  
  27.          $("#div2 span").draggable (  
  28.             { distance: 50 }  
  29.          );  
  30.          $("#div3 span").draggable (  
  31.             { delay: 200 }  
  32.          );  
  33.  </script>  
  34.    </body>  
  35. </html>  

jQuery UI Draggable() method example 3

Move content by duplicating:
Let's take an example to demonstrate how to move an element that is clone of the selected element. This is done by using option helper with value clone.
  1.  <!DOCTYPE html>  
  2. <head>  
  3.    <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  4.    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  5.    <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  6. </head>  
  7. <body>  
  8.    <div id="div6" style="border:solid 1px;background:lightyellow; height:30px;">  
  9.       <span>You can duplicate me....</span>  
  10.    </div>  
  11.    <script>  
  12.       $("#div6 span").draggable ({  
  13.          helper : "clone"  
  14.       });  
  15.    </script>  
  16. </body>  
  17. </html>  

jQuery UI Draggable() method example 4

Get current option value:
Let's take an example to show how you can get a value of any option at any time during your script execution. The value of cursor and cursorAt option is set at the time of execution.
  1. <!DOCTYPE html>  
  2. <head>  
  3.    <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  4.    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  5.    <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  6. </head>  
  7. <body>  
  8.    <div id="divX" style="border:solid 1px;background:lightpink; height:30px;">  
  9.       <span>Click on me to see cursor type.</span>  
  10.    </div>  
  11.    <script>  
  12.       /* First make the item draggable */  
  13.       $("#divX span").draggable();  
  14.         
  15.       $("#divX span").bind('click', function( event ){  
  16.           var cursor = $( "#divX span" ).draggable( "option", "cursor" );  
  17.           var cursorAt = $( "#divX span" ).draggable( "option", "cursorAt" );  
  18.           alert("Cursor type - " + cursor + ", cursorAt - " + cursorAt);  
  19.      });  
  20.    </script>  
  21. </body>  
  22. </html>  

Second Method

The draggable (action, params) method is used to perform an action like prevent displacement. Here action is specified as a string and one or more params can be provided based on the given action.
Syntax:
  1. $(selector, context).draggable ("action", [params]);  
Following is a list of actions used with this method:
ActionDescription
destroy()It is used to remove drag functionality completely. The elements are no longer movable. This will return the element back to its pre-init state.
disable()It is used to disable drag functionality. Elements cannot be moved until the next call to the draggable("enable") method.
enable()It is used to reactivates drag management. The elements can be moved again.
option(optionname)It gets the value currently associated with the specified optionname. Here optionname is name of the option to get and is of type string.
option()It gets an object containing key/value pairs representing the current draggable options hash.
option(optionname, value)It sets the value of the draggable option associated with the specified optionname. Here optionname is the name of the option to set and value is the value to set for the option.
option(options)It sets one or more options for the draggable. Here options is a map of option-value pairs to set.
widget()It returns a jQuery object containing the draggable element.

jQuery UI Draggable() method example 5

Let's take an example to see the use of actions from the above table. The following example demonstrates the use of "disable" and "enable" action.
  1. <!DOCTYPE html>  
  2.    <head>  
  3.       <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  4.       <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  5.       <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  6.    </head>  
  7.    <body>  
  8.       <div id="div7" style="border:solid 1px;background-color:pink;">  
  9.          <span>Dragging is disabled. You can't move me!</span><br><br>  
  10.       </div>  
  11.       <div id="div8" style="border:solid 1px;background-color:yellow;">  
  12.          <span>Dragging is enabled. Move Me!</span><br><br>  
  13.       </div>  
  14.       <script>  
  15.          $("#div7 span").draggable ();  
  16.          $("#div7 span").draggable ('disable');  
  17.          $("#div8 span").draggable ();  
  18.          $("#div8 span").draggable ('enable');  
  19.       </script>  
  20.    </body>  
  21. </html>  

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...