JQueryUI - Day 3 ( Droppable )

In this article you will learn about Droppable in JQueryUI. This is part 3 of the article series.

Before reading this article, I would recommend reading the following previous parts:

Droppable widget create a target for draggable element. jQueryUI droppable widget makes the selected element to draggable and droppable widget define which draggable elements are accepted by a droppable element. Now we read about the options, methods and events that are associated with a droppable element.

Firstly, create a draggable and droppable element.

HTML Code

  1. <divid="drag">  
  2.     <spanid="span1">Draggable Element</span>  
  3.         <br/>  
  4.     <spanid="span2">Drag Me</span>  
  5. </div>  
  6. <divid="drop">  
  7.     <spanid="span2">Drop Here!</span>  
  8. </div> jQuery Code: $(document).ready(function () { $("#drag").draggable(); $("#drop").droppable(); });  
CSS Code
  1. <style>  
  2.     #drag {  
  3.         background-color: paleturquoise;  
  4.         border-style: groove;  
  5.         border-color: chocolate;  
  6.         font-size: 20px;  
  7.         width: 230px;  
  8.         height: 100px;  
  9.         margin-left: 150px;  
  10.         margin-top: 200px;  
  11.         text-align: center;  
  12.     }  
  13.      
  14.     #drop {  
  15.         background-color: burlywood;  
  16.         border-style: groove;  
  17.         border-color: CaptionText;  
  18.         font-size: 26px;  
  19.         width: 330px;  
  20.         height: 200px;  
  21.         margin-left: 150px;  
  22.         margin-top: 200px;  
  23.         text-align: center;  
  24.     }  
  25.      
  26.     #span1 {  
  27.         font-size: 26px;  
  28.         color: darkolivegreen;  
  29.     }  
  30. </style>  
Design of web Page
Design of web Page

Options

accept Option:

The accept option define  which draggable element is  accepted by the droppable. A function is called for each draggable on the page. This method returns true if the draggable should be accepted.

Example:
  1. $("#drop").droppable({  
  2.    accept: "#drag"  
  3. });  
In this code droppable element with “drop” id will accept the draggable element whose id is “drag”.

activeClasses Option:

The activeClasses option specify that, the class will be added to the droppable while an acceptable draggable is being dragged.

Example:
  1. $("#drop").droppable(  
  2. {  
  3.     accept: "#drag",  
  4.     activeClass: "ui-state-highlight"  
  5. });  
Output:

activeClasses Option

addClasses Option:

If addClasses option is set to “false” then “ui-droppable” will not be added to droppable element. This may be desired as a performance optimization when calling a large number of elements.

Example:
  1. $("#drop").droppable(  
  2. {  
  3.     accept: "#drag",  
  4.     addClasses: false  
  5. });  
disabled Option:

The disabled option disable the droppable element if set to “true”.

Example:
  1. $("#drop").droppable(  
  2. {  
  3.     accept: "#drag",  
  4.     activeClass: "ui-state-highlight",  
  5.     disabled: true  
  6. });  
Output:
activeClasses

We can see the mage background color in the preceding image of droppable element even though the activeClasses option is defined. This happened because disable option is set to “true”.

greddy Option:

If greddy option is set to “true” then greddy option can be used to identify which droppable receive the draggable element among the nested elements. By default, when an element is dropped on nested droppable , each droppable will receive the element. So by defining the option to “true” any parent element will not receive the droppable element.

Example:
  1. $("#drop").droppable(  
  2. {  
  3.     accept: "#drag",  
  4.     greddy: true  
  5. });  
hoverClass Option:

The hoverClass option define the class that will be added when a draggable element will hover over the droppable element.

Example:
  1. $("#drop").droppable(  
  2. {  
  3.     accept: "#drag",  
  4.     hoverClass: "ui-drop-hover"  
  5. });  
  6. Ui - drop - hover class is defined as.ui - drop - hover  
  7. {  
  8.     background - color: #FFFF55!important;  
  9.     border: 2 pxsolid# bbb;  
  10. }  
disabled Option

scope Option:

The scope option is used to group sets of draggable and droppable items. A draggable with the same scope value as a droppable will be accepted.

Example:
  1. $("#drag").draggable(  
  2. {  
  3.     scope: "accept"  
  4. });  
  5. $("#drop").droppable(  
  6. {  
  7.     accept: "#drag",  
  8.     activeClass: "ui-state-highlight",  
  9.     scope: "accept"  
  10. });  
Output:

scope Option

In above code “ui-state-highlight” classes are activated because both draggable and droppable element have same scope name . Let’s try to change the scope of any element.
  1. $("#drag").draggable(  
  2. {  
  3.     scope: "accept1"  
  4. });  
  5. $("#drop").droppable(  
  6. {  
  7.     accept: "#drag",  
  8.     activeClass: "ui-state-highlight",  
  9.     scope: "accept"  
  10. });  
Output:

element

Now we can see that background color is not changed because this time scope variable of both the elements have different values so “ui-state-highlight” class did not activated.

tolerance Option:

The tolerance option specify that which mode is used to test that draggable element is hovering over the droppable element. The tolerance option can have any value among the below options.

Fit: When draggable element overlaps the droppable element.

Example:
  1. $("#drop").droppable(  
  2. {  
  3.     accept: "#drag",  
  4.     hoverClass: "ui-state-highlight",  
  5.     tolerance: "fit"  
  6. });  
Output:

tolerance Option

Intersect: When draggable element overlaps the droppable at least 50% in both directions.

Example:
  1. $("#drop").droppable(  
  2. {  
  3.     accept: "#drag",  
  4.     hoverClass: "ui-state-highlight",  
  5.     tolerance: "intersect"  
  6. });  
Output:

Intersect

Pointer: When mouse pointer overlaps the droppable.

Example:
  1. $("#drop").droppable(  
  2. {  
  3.     accept: "#drag",  
  4.     hoverClass: "ui-state-highlight",  
  5.     tolerance: "pointer"  
  6. });  
Output:
Pointer

Touch: When draggable element overlaps the droppable any amount.

Example:
  1. $("#drop").droppable(  
  2. {  
  3.     accept: "#drag",  
  4.     hoverClass: "ui-state-highlight",  
  5.     tolerance: "touch"  
  6. });  
Output

Touch

Methods

destroy Method:

The destroy() method removes the complete droppable functionality and return the element into its pre initial state.

Example:
  1. $("#drop").droppable("destory");  
Disable Method:

The disable() method disables the droppable element.

Example:
  1. $("#drop").droppable("disable");  
Enable method:

The enable method enables the droppable element.

Example:
  1. $("#drop").droppable("enable");  
option Method:

The option method is used to get or set the option for the droppable elements.

Option as getter:

We can get the value currently associated with the specified option name.

Example:
  1. $("#drag").draggable();  
  2. $("#drop").droppable({ disabled: "flase" });  
  3. alert("Disabled is set to=" + $("#drop").droppable("option""disabled"));  
Output:

Option as getter

Option as setter:

We can assign value of any option by passing the option name and it’s value.

Example:

option as setter

Events

activate (event, ui) Event:

The activate event is called when an accepted draggable start dragging. The event object define the event and ui parameter define several types of object like the following,
  • Draggable: Represent the dragging object.
  • Helper: Define the helper that is being dragged.
  • Position: Current css position
  • Offset: Current offset position.

Example:

  1. ("#drop").droppable(  
  2. {  
  3.     activate: function(event, ui)  
  4.     {  
  5.         alert("Left Position is=" + ui.position.left + "\n" + "top Position is=" + ui.position.top + "\n" + "Left offset is=" + ui.offset.left + "\n" + "top offset is=" + ui.offset.top + "\n")  
  6.     }  
  7. });  
Output:

Events

create (event, ui ) Event:

This event is triggered when a droppable element is created.

Example:
  1. $("#drop").droppable({  
  2.    create: function (event, ui) {  
  3.       alert("Droppable Has Been Created");  
  4.    }  
  5. });  
deactivate(event, ui) Event:

This event is triggered when an accepted draggable stop dragging. The event object define the event and ui parameter define several types of object like.
  • Draggable: Represent the dragging object.
  • Helper: Define the helper that is being dragged.
  • Position: Current css position
  • Offset: current offset position.

Example:

  1. $("#drop").droppable({  
  2. deactivate: function (event, ui) {  
  3. alert("Left Position is=" + ui.position.left + "\n" +  
  4. "top Position is=" + ui.position.top + "\n" +  
  5. "Left offset is=" + ui.offset.left + "\n" +  
  6. "top offset is=" + ui.offset.top + "\n")}  
Output:

message

drop(even, ui) Event:

The drop event is called when an accepted draggable is dropped on the droppable. The event object define the event and ui parameter define several types of object like.
  • Draggable: Represent the dragging object.
  • Helper: Define the helper that is being dragged.
  • Position: Current css position
  • Offset: current offset position.

Example:

  1. drop: function (event, ui) {  
  2. alert("Left Position is=" + ui.position.left + "\n" +  
  3. "top Position is=" + ui.position.top + "\n" +  
  4. "Left offset is=" + ui.offset.left + "\n" +  
  5. "top offset is=" + ui.offset.top + "\n")}  
Output:

run

out (event, ui) Event:

The out event is triggered when accepted draggable is dragged out of the droppable scope.

Example:
  1. $("#drop").droppable({  
  2. out: function (event, ui) {  
  3.       //Event is occured when dragged element will become out of scope  
  4.    }  
  5. });  
over(event, ui) Event:

The over event is triggered when accepted draggable is dragged over the droppable. The event object define the event and ui parameter define several types of object like.
  • Draggable: Represent the dragging object.
  • Helper: Define the helper that is being dragged.
  • Position: Current css position
  • Offset: Current offset position.

Example:

  1. $("#drop").droppable(  
  2. {  
  3.     over: function(event, ui)  
  4.     {  
  5.         alert("Left Position is=" + ui.position.left + "\n" + "top Position is=" + ui.position.top + "\n" + "Left offset is=" + ui.offset.left + "\n" + "top offset is=" + ui.offset.top + "\n")  
  6.     }  
  7. });  
Output:

Output

Today we learned about Droppable widget of jQueryUI. In next article we will learn about another jQueryUI widget.

Thanks for reading the article.