concept draggable in category jQuery

appears as: draggables, draggables, draggable, draggable
jQuery UI in Action

This is an excerpt from Manning's book jQuery UI in Action.

The last step is to convert these new drop zones to droppable widgets. You set an accept option and a drop event callback. The droppable widget’s accept option controls which draggable widgets should be accepted. It supports two types of arguments. The first is a CSS selector—for example, "*" allows all draggables and "#foo" only allows draggables with an id of "foo". The second, and the one you use , is a function that must return a Boolean indicating whether the draggable should be accepted. Your version is shown here:

accept: function( draggable ) {
    return $( this ).text() == draggable.attr( "id" );
}

The context of the accept option (this) is set to the droppable element and is passed the draggable element as an argument. Recall that you set both the content of the droppables and the id of the draggables equal to the color’s name. With that in mind, this function is saying, “when the text of the droppable matches the id of the draggable, the draggable should be accepted; otherwise, it should be rejected.”

Because the accept option enforces the color section, your drop event is called only after the user makes valid selections. The drop event’s ui parameter contains a reference to the draggable object in its draggable property. You grab the background-color from the draggable element and set it as the background of the droppable one as shown in the following code and at . A "filled" class name is also added; you’ll use that later to determine when the game is complete:

The last option you set is tolerance , which determines which algorithm the widgets should use for determining whether a draggable is indeed over a droppable. It has four possible values:

  • "fit" —Drop is valid if the draggable overlaps the droppable completely.
  • "intersect" —Drop is valid if the draggable overlaps the droppable by at least 50% vertically and horizontally. This is the default setting.
  • 5.2. Droppable: creating containers that accept draggables

    Most UIs that use draggables also use droppables. Consider the OS’s file interface. When you start dragging files, you can move them to alternate directories, move them to the trash bin, move them to other applications, and more.