1
I’m trying to exchange two images when a user clicks on one image and drags to the other.
However, my code does not put the images in the correct position, it leaves the destination image in the same place and puts the image that was dragged in the <div>
next, superimposing another image.
$(".elemento").draggable();
$(".containerImg").droppable({
drop: function(event, ui) {
var dropped = ui.draggable;
var droppedOn = event.target;
$(dropped).css({top: 0,left: 0}).appendTo(droppedOn);
$(droppedOn.querySelector('img')).css({top: 0,left: 0}).appendTo(dropped.parent());
},
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="painel-tabuleiro">
<div class="col-1">
<div class="containerImg"><img class="elemento" src="imagem/1.png"/></div>
<div class="containerImg"><img class="elemento" src="imagem/1.png"/></div>
<div class="containerImg"><img class="elemento" src="imagem/1.png"/></div>
</div>
<div class="col-2">
<div class="containerImg"><img class="elemento" src="imagem/2.png"/></div>
<div class="containerImg"><img class="elemento" src="imagem/2.png"/></div>
<div class="containerImg"><img class="elemento" src="imagem/2.png"/></div>
</div>
<div class="col-3">
<div class="containerImg"><img class="elemento" src="imagem/3.png"/></div>
<div class="containerImg"><img class="elemento" src="imagem/3.png"/></div>
<div class="containerImg"><img class="elemento" src="imagem/3.png"/></div>
</div>
<div class="col-4">
<div class="containerImg"><img class="elemento" src="imagem/4.png"/></div>
<div class="containerImg"><img class="elemento" src="imagem/4.png"/></div>
<div class="containerImg"><img class="elemento" src="imagem/4.png"/></div>
</div>
</div>
I don’t recommend using draggable which is only for moving elements. Try using Sortable. You can help more than draggable: https://jqueryui.com/sortable/
– NDelavi
I tried sortable, but I couldn’t exchange the elements between the Divs, only the elements of the Divs itself
– Henrique
Henrique, I put in the answer, but look at this fiddle and see if this is the result you’re looking for: https://jsfiddle.net/ndelavi/w7k1re0v/16/
– NDelavi