cha*_*m15 4 javascript jquery jquery-ui
我有一个我想允许被拖到任何地方的元素.但是,如果它被丢弃到拒绝它的droppable(通过接受),我希望它在拖动之前恢复到原始位置.我怎么能做到这一点?
编辑:更具体一点:我希望满足两个条件,以便将项目移回:1.它被放置在一个droppable和2. droppable没有接受所述项目.Draggable的还原选项仅检查条件2.
我有一些我认为应该工作的代码.请注意,它不使用该accept选项指定droppables将接受哪些可拖动项.相反,您必须检查是否应在drop事件中手动拒绝可拖动.
$(document).ready(function() {
$('.droppable-class').droppable({
drop: function(event, ui) {
// check whether or not draggable should be rejected here...
if (...) {
ui.draggable.data('rejected', true);
}
}
});
$('.draggable-class').draggable({
revert: false,
start: function(event, ui) {
ui.helper.data('rejected', false);
ui.helper.data('original-position', ui.helper.offset());
},
stop: function(event, ui) {
if (ui.helper.data('rejected') === true) {
ui.helper.offset(ui.helper.data('original-position'));
}
}
});