I have a code that puts an icon on the screen every time you double click on the screen. These icons that are created are draggable. The problem is that I can drop the icons on top of each other and I do not know what to do to prevent the program from letting it drop an icon on top of another. I would like it when it releases the icon on another one it returns to its original place and rejects it. I tried greedy: true on the droppable, and also with stopProgagation but it does not work.
Here I leave the code:
$(document).ready(function(){
var i = 0;
var counter = 0;
function newTimer(x,y) {
var id = "#" + "timer" + counter;
var clock = '<div id ="timer'+ counter + ' " class="ui-widget-content timer" '
+ ' style = "left: ' + x + 'px ; top: ' + y + 'px "><i class = "fa fa-clock-o fa-4x" aria-hidden = "true"> </i></div>';
$('#content').append($(clock));
$(".timer").draggable( {
cointainment: "#content",
snap: true,
revert: "invalid"});
counter++;
}
$("#content").droppable({
greedy: true,
accept: ".timer",
drop: function( event, ui ) {
i++;
console.log("Lo soltaste " + i);
}
});
$("#content").on( "dblclick", function(e) {
var pos = $(this).offset();
var posX = e.pageX - pos.left;
var posY = e.pageY - pos.top;
console.log("posicion x = " + posX);
console.log("posicion y = " + posY);
newTimer(posX,posY);
});
});
I would appreciate if you could guide me about it. The Jquery versions are jquery-2.2.4 and jquery-ui-1.11.4.