Вот то, что у меня есть в минуту для моих перетаскиваемых и отбрасываемых разделов:
$('.planets').draggable({
opacity: .4,
create: function(){$(this).data('position',$(this).position())},
cursorAt:{left:15},
cursor:'move',
start:function(){$(this).stop(true,true)},
revert : 'invalid'
});
$('.targets').droppable({
greedy: true, ///////////////
drop: function( event, ui ) {
$( this ).addClass( "dropped-highlight" );
$(this).droppable('option', 'accept', ui.draggable); ////////
$(this).append(ui.draggable); /////////
snapToMiddle(ui.draggable,$(this)); //This function snaps the draggable to the middle of the droppable
},
out: function( event, ui ) {
$(this).removeClass( "dropped-highlight" );
$(this).droppable('option', 'accept', '.planets'); /////////
}
});
На данный момент я могу складывать несколько перетаскиваемых объектов в один droppable. Я только хочу разрешить ANY ONE droppable в перетаскиваемом за раз. После удаления перетаскиваемого объекта в эту область может войти новый. Строки кода с ////// / - это те, которые я недавно добавил, чтобы попытаться достичь этого.
Редактировать: Это работает!
$('.planets').draggable({
opacity: .4,
create: function(){$(this).data('position',$(this).position())},
cursorAt:{left:15},
cursor:'move',
start:function(){$(this).stop(true,true)},
revert : 'invalid'
});
$('.targets').droppable({
drop: function( event, ui ) {
if(!$(this).hasClass('dropped-highlight')){
$( this ).addClass( "dropped-highlight" );
$(this).droppable('option', 'accept', ui.draggable);
}
},
out: function( event, ui ) {
$(this).droppable('option', 'accept', '.planets');
$(this).removeClass( "dropped-highlight" );
}
});
Проверьте, присутствует ли класс "отброшенного выделения" перед добавлением этого элемента и удалите его в "выпадающей" части объявления с возможностью удаления.
Что-то вроде (псевдокод):
if! this.hasClass('drop-highlight') {
(ваш код)
}
и в drop: this.removeClass('drop-highlight').
У вас уже есть вторая вещь, просто добавьте первую.
if !$(this).hasClass("dropped-highlight"){
$( this ).addClass( "dropped-highlight" );
$(this).droppable('option', 'accept', ui.draggable);
}