恢复时jQuery ui.draggable事件/状态

flo*_*los 4 jquery events jquery-ui draggable

如果可拖动的元素被还原,有没有办法获取信息?

我坚持这个.我想再次使元素可以放置,但只有当躺在那里的可拖动移动到别处时(意味着不会恢复).

mbe*_*dub 22

我发现有客场得到一个对象是否已经恢复与否的信息.它内置于jQuery中,但显然没有很好的文档记录.

本质上,它是通过使用回调函数来完成可拖动对象的恢复选项来完成的.

类似于以下内容:

$(".myselector").draggable(
{
  revert: function(droppableObj)
  {
     //if false then no socket object drop occurred.
     if(droppableObj === false)
     {
        //revert the .myselector object by returning true
        return true;
     }
     else
     {
        //droppableObj was returned,
        //we can perform additional checks here if we like
        //alert(droppableObj.attr('id')); would work fine

        //return false so that the .myselector object does not revert
        return false;
     }
  }
});
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅http://www.agilepro.com/blog/2009/12/while-this-functionality-is-built-into.html.


Pet*_*dIt 9

看起来jQuery UI不支持它,所以你可以自己添加它:

$.ui.draggable.prototype._mouseStop = function(event) {
    //If we are using droppables, inform the manager about the drop
    var dropped = false;
    if ($.ui.ddmanager && !this.options.dropBehaviour)
        dropped = $.ui.ddmanager.drop(this, event);

    //if a drop comes from outside (a sortable)
    if(this.dropped) {
        dropped = this.dropped;
        this.dropped = false;
    }

    if((this.options.revert == "invalid" && !dropped) || (this.options.revert == "valid" && dropped) || this.options.revert === true || ($.isFunction(this.options.revert) && this.options.revert.call(this.element, dropped))) {
        var self = this;
        self._trigger("reverting", event);
        $(this.helper).animate(this.originalPosition, parseInt(this.options.revertDuration, 10), function() {
            event.reverted = true;
            self._trigger("stop", event);
            self._clear();
        });
    } else {
        this._trigger("stop", event);
        this._clear();
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)

允许你这样做:

$(document).ready(function($) {
    $('#draggable').draggable({
        revert: true,
        reverting: function() {
            console.log('reverted');
        },
        stop: function(event) {
            if (event.reverted) {
                console.log('reverted');
            }
        }
    });
});
Run Code Online (Sandbox Code Playgroud)