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.
看起来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)