jQuery UI API - Droppable Widget
Category
Usage
**Description:** Create targets for draggable widgets.
**Version added:** 1.0
Dependencies:
**Note:** The jQuery UI Droppable plugin makes selected elements droppable (meaning they accept a drop after being dragged over). You can specify individually which draggable each one will accept.
Quick navigation
| Option | Method | Events |
|---|---|---|
| Option | Type | Description | Default value |
|---|---|---|---|
| accept | Selector or Function() | Controls which draggable elements can be accepted by the droppable. |
支持多个类型:
true。Code example:
Initialize the accordion with the specified accept the droppable's option:
$( ".selector" ).droppable({ accept: ".special" });
After initialization, get or set the accept Option:
// getter var accept = $( ".selector" ).droppable( "option", "accept" );| "*"| onSelect | event | activeClass | String | If this option is specified, the class is added to the droppable while an acceptable draggable is being dragged.// setter $( ".selector" ).droppable( "option", "accept", ".special" );
Code example:
Initialize the accordion with the specified activeClass the droppable's option:
$( ".selector" ).droppable({ activeClass: "ui-state-highlight" });
After initialization, get or set the activeClass Option:
// getter var activeClass = $( ".selector" ).droppable( "option", "activeClass" );| false| onSelect | event | addClasses | Boolean | If set to// setter $( ".selector" ).droppable( "option", "activeClass", "ui-state-highlight" );
false, this will prevent the ui-droppable class from being added. This helps performance when calling .droppable() on hundreds of elements.
Code example:
Initialize the accordion with the specified addClasses the droppable's option:
$( ".selector" ).droppable({ addClasses: false });
After initialization, get or set the addClasses Option:
// getter var addClasses = $( ".selector" ).droppable( "option", "addClasses" );| true| onSelect | event | disabled | Boolean | If set to// setter $( ".selector" ).droppable( "option", "addClasses", false );
true, the droppable is disabled.
Code example:
Initialize the accordion with the specified disabled the droppable's option:
$( ".selector" ).droppable({ disabled: true });
After initialization, get or set the disabled Option:
// getter var disabled = $( ".selector" ).droppable( "option", "disabled" );| false| onSelect | event | greedy | Boolean | By default, when an element is dropped on nested droppables, each droppable will receive the element. However, by setting this option to// setter $( ".selector" ).droppable( "option", "disabled", true );
true, any parent droppable will not receive the element. drop Events will still fire as usual, but the event.target is checked to see which droppable receives the draggable element.
Code example:
Initialize the accordion with the specified greedy the droppable's option:
$( ".selector" ).droppable({ greedy: true });
After initialization, get or set the greedy Option:
// getter var greedy = $( ".selector" ).droppable( "option", "greedy" );| false| onSelect | event | hoverClass | String | If this option is specified, the class is added to the droppable when an acceptable draggable is hovered over it.// setter $( ".selector" ).droppable( "option", "greedy", true );
Code example:
Initialize the accordion with the specified hoverClass the droppable's option:
$( ".selector" ).droppable({ hoverClass: "drop-hover" });
After initialization, get or set the hoverClass Option:
// getter var hoverClass = $( ".selector" ).droppable( "option", "hoverClass" );| false| onSelect | event | scope | String | Used to group sets of draggable and droppable items, in addition to the droppable's// setter $( ".selector" ).droppable( "option", "hoverClass", "drop-hover" );
accept option. A draggable with the same scope value as a droppable will be accepted by that droppable.
Code example:
Initialize the accordion with the specified scope the droppable's option:
$( ".selector" ).droppable({ scope: "tasks" });
After initialization, get or set the scope Option:
// getter var scope = $( ".selector" ).droppable( "option", "scope" );| "default"| onSelect | event | tolerance | String | Specifies which mode to use for testing whether a draggable is hovering over a droppable. Possible values:// setter $( ".selector" ).droppable( "option", "scope", "tasks" );
"fit": The draggable completely overlaps the droppable."intersect": The draggable overlaps the droppable by at least 50% in both directions."pointer": The mouse pointer overlaps the droppable."touch": The draggable overlaps the droppable by any amount.
Code example:
Initialize the accordion with the specified tolerance the droppable's option:
$( ".selector" ).droppable({ tolerance: "fit" });
After initialization, get or set the tolerance Option:
// getter var tolerance = $( ".selector" ).droppable( "option", "tolerance" );| "intersect" |// setter $( ".selector" ).droppable( "option", "tolerance", "fit" );
| Method | Return | Description |
|---|---|---|
| destroy() | jQuery (plugin only) | Completely remove the droppable functionality. This returns the element to its pre-initialization state. |
Code example:
Invoke the destroy method:
$( ".selector" ).droppable( "destroy" );| onSelect | event | disable() | jQuery (plugin only) | Disable the droppable.
- This method accepts no arguments.
Code example:
Invoke the disable method:
$( ".selector" ).droppable( "disable" );| onSelect | event | enable() | jQuery (plugin only) | Enable the droppable.
- This method accepts no arguments.
Code example:
Invoke the enable method:
$( ".selector" ).droppable( "enable" );| onSelect | event | option( optionName ) | Object | Gets the value currently associated with the specified `optionName`.
- optionName
Type: String
Description: the name of the option to get.
Code example:
Invoke the method:
var isDisabled = $( ".selector" ).droppable( "option", "disabled" );| onSelect | event | option() | PlainObject | Get an object containing key/value pairs representing the current droppable options hash.
- This method accepts no arguments.
Code example:
Invoke the method:
var options = $( ".selector" ).droppable( "option" );| onSelect | event | option( optionName, value ) | jQuery (plugin only) | Set the value of the droppable option associated with the specified `optionName`.
- optionName
Type: String
Description: the name of the option to set. - value
Type: Object
Description: the value to set for the option.
Code example:
Invoke the method:
$( ".selector" ).droppable( "option", "disabled", true );| onSelect | event | option( options ) | jQuery (plugin only) | Set one or more options for the droppable.
- options
Type: Object
Description: the option-value pairs to set.
Code example:
Invoke the method:
$( ".selector" ).droppable( "option", { disabled: true } );
| onSelect | event | widget() | jQuery | Return a `jQuery` object containing the droppable element.
- This method accepts no arguments.
Code example:
Invoke the widget method:
var widget = $( ".selector" ).droppable( "widget" );|
| Events | Type | Description |
|---|---|---|
| activate( event, ui ) | dropactivate | Fired when an acceptable draggable starts dragging. This is useful if you want the droppable to "light up" when something is dropped on it. |
Type: Event
Type: Object
- draggable
Type: jQuery
Description: A jQuery object representing the draggable element. - helper
Type: jQuery
Description: A jQuery object representing the dragged helper. - position
Type: Object
Description: The current CSS position of the draggable helper, e.g.{ top, left }object. - offset
Type: Object
Description: The current offset position of the draggable helper, e.g.{ top, left }object.
Code example:
Initialize the droppable with the activate callback specified:
$( ".selector" ).droppable({
activate: function( event, ui ) {}
});
Bind an event listener to the dropactivate event:
$( ".selector" ).on( "dropactivate", function( event, ui ) {} );
| onSelect | event | create( event, ui ) | dropcreate | Fired when the droppable is created.
- event
Type: Event - ui
Type: Object
Note:ui The object is empty; it is included here for consistency with other events.
Code example:
Initialize the droppable with the create callback specified:
$( ".selector" ).droppable({
create: function( event, ui ) {}
});
Bind an event listener to the dropcreate event:
$( ".selector" ).on( "dropcreate", function( event, ui ) {} );
| onSelect | event | deactivate( event, ui ) | dropdeactivate | Fired when an acceptable draggable stops dragging.
- event
Type: Event - ui
Type: Object- draggable
Type: jQuery
Description: A jQuery object representing the draggable element. - helper
Type: jQuery
Description: A jQuery object representing the dragged helper. - position
Type: Object
Description: The current CSS position of the draggable helper, e.g.{ top, left }object. - offset
Type: Object
Description: The current offset position of the draggable helper, e.g.{ top, left }object.
- draggable
Code example:
Initialize the droppable with the deactivate callback specified:
$( ".selector" ).droppable({
deactivate: function( event, ui ) {}
});
Bind an event listener to the dropdeactivate event:
$( ".selector" ).on( "dropdeactivate", function( event, ui ) {} );
| onSelect | event | drop( event, ui ) | drop | Fired when an acceptable draggable is dropped on the droppable (based on the `tolerance` option).
- event
Type: Event - ui
Type: Object- draggable
Type: jQuery
Description: A jQuery object representing the draggable element. - helper
Type: jQuery
Description: A jQuery object representing the dragged helper. - position
Type: Object
Description: The current CSS position of the draggable helper, e.g.{ top, left }object. - offset
Type: Object
Description: The current offset position of the draggable helper, e.g.{ top, left }object.
- draggable
Code example:
Initialize the droppable with the drop callback specified:
$( ".selector" ).droppable({
drop: function( event, ui ) {}
});
Bind an event listener to the drop event:
$( ".selector" ).on( "drop", function( event, ui ) {} );
| onSelect | event | out( event, ui ) | dropout | Fired when a draggable is dragged out of the droppable (based on the `tolerance` option).
- event
Type: Event - ui
Type: Object
Note:ui The object is empty; it is included here for consistency with other events.
Code example:
Initialize the droppable with the out callback specified:
$( ".selector" ).droppable({
out: function( event, ui ) {}
});
Bind an event listener to the dropout event:
$( ".selector" ).on( "dropout", function( event, ui ) {} );
| onSelect | event | over( event, ui ) | dropover | Fired when an acceptable draggable is dragged over the droppable (based on the `tolerance` option).
- event
Type: Event - ui
Type: Object- draggable
Type: jQuery
Description: A jQuery object representing the draggable element. - helper
Type: jQuery
Description: A jQuery object representing the dragged helper. - position
Type: Object
Description: The current CSS position of the draggable helper, e.g.{ top, left }object. - offset
Type: Object
Description: The current offset position of the draggable helper, e.g.{ top, left }object.
- draggable
Code example:
Initialize the droppable with the over callback specified:
$( ".selector" ).droppable({
over: function( event, ui ) {}
});
Bind an event listener to the dropover event:
$( ".selector" ).on( "dropover", function( event, ui ) {} );
|
Example
A pair of draggable and droppable elements.
可放置小部件(Droppable Widget)演示
请放置到这里!
请拖拽我!