Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery UI API - Droppable Widget

jQuery UI API - Droppable Widget

Category

Interactions

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

accept

activeClass
addClasses
disabled
greedy
hoverClass
scope
tolerance
|
destroy
disable
enable
option
widget
|
activate
create
deactivate
drop
out
over
|
Option Type Description Default value
accept Selector or Function() Controls which draggable elements can be accepted by the droppable.
 

支持多个类型:

  • Selector: A selector specifying which draggable elements can be accepted by the droppable.
  • Function(): A function that will be called for each draggable on the page (passed as the first argument to the function). If the draggable is accepted, the function must return 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" );
    
    

    // setter $( ".selector" ).droppable( "option", "accept", ".special" );     

    | "*"| onSelect | event | activeClass | String | If this option is specified, the class is added to the droppable while an acceptable draggable is being dragged.

    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" );

    // setter $( ".selector" ).droppable( "option", "activeClass", "ui-state-highlight" );     

    | false| onSelect | event | addClasses | Boolean | If set to 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" );

    // setter $( ".selector" ).droppable( "option", "addClasses", false );     

    | true| onSelect | event | disabled | Boolean | If set to 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" );

    // setter $( ".selector" ).droppable( "option", "disabled", true );     

    | 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 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" );

    // setter $( ".selector" ).droppable( "option", "greedy", true );     

    | 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.

    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" );

    // setter $( ".selector" ).droppable( "option", "hoverClass", "drop-hover" );     

    | false| onSelect | event | scope | String | Used to group sets of draggable and droppable items, in addition to the droppable's 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" );

    // setter $( ".selector" ).droppable( "option", "scope", "tasks" );     

    | "default"| onSelect | event | tolerance | String | Specifies which mode to use for testing whether a draggable is hovering over a droppable. Possible values:

    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" );
    
    

    // setter $( ".selector" ).droppable( "option", "tolerance", "fit" );     

    | "intersect" |

    Method Return Description
    destroy() jQuery (plugin only) Completely remove the droppable functionality. This returns the element to its pre-initialization state.
     
  • This method accepts no arguments.
  • Code example:

    Invoke the destroy method:

    $( ".selector" ).droppable( "destroy" );
    
        
    | onSelect | event | disable() | jQuery (plugin only) | Disable the droppable.

    Code example:

    Invoke the disable method:

    $( ".selector" ).droppable( "disable" );
    
        
    | onSelect | event | enable() | jQuery (plugin only) | Enable the droppable.

    Code example:

    Invoke the enable method:

    $( ".selector" ).droppable( "enable" );
    
        
    | onSelect | event | option( optionName ) | Object | Gets the value currently associated with the specified `optionName`.

    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.

    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`.

    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.

    Code example:

    Invoke the method:

    $( ".selector" ).droppable( "option", { disabled: true } );
    
        
    | onSelect | event | widget() | jQuery | Return a `jQuery` object containing the droppable element.

    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.
     
  • 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.
  • 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.

    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.

    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).

    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).

    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).

    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)演示
    
      
    
      
    
      
    
      
    
    
    
    
    
     
    
    
    请放置到这里!
    请拖拽我!