The decision made is cleaner, but has 1 problem. As soon as Draggable falls over the green zone, it will beocome Droppable even on a white background ... (Note revert: false ) Updates this answer with the correct code.
Demo2
$('.draggable').draggable({ revert: 'invalid' }); $('#droppable, #block').droppable({ accept: '.draggable', drop: function( event, ui ) { if(!ui.draggable.data('original')){ ui.draggable.data('original',ui.draggable.draggable("option", "revert")); } if (this.id == 'block') { ui.draggable.draggable({ revert: true }); } else { ui.draggable.draggable({ revert: ui.draggable.data('original') }); } } });
This question seems to already have an answer. but here is my attempt to solve the same problem.
Demo
$('#block').droppable({ accept: '.draggable', drop: function (event, ui) { if (ui.draggable.data('revert')) { ui.draggable.data('revert', false); var old = ui.draggable.draggable("option", "revert"); ui.draggable.draggable("option", "revert", true); setTimeout(function () { ui.draggable.draggable("option", "revert", old); }, 100); } }, out: function (event) { $('.draggable').data('revert', false); }, over: function (event,ui) { ui.draggable.data('revert', true); } }); $('#droppable').droppable({ accept: function (elem) { if ($('.draggable').data('revert')) { return false; } return elem.hasClass("draggable"); } }); $('.draggable').draggable({ revert: 'invalid' });
source share