Drag & Drop with jQuery does not place the element in the right DIV

Asked

Viewed 258 times

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/

  • I tried sortable, but I couldn’t exchange the elements between the Divs, only the elements of the Divs itself

  • 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/

1 answer

1


Based on our comments, I took a look around the internet about the Sortable and found this post on Stack in English:

Stackoverflow in English

Basically you will use the Sortable within a table and modify your CSS to use inline-block to make

Here is jsFiddle for you to base and see working:

table + sortable

Here the code:

HTML

<table>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
    </tr>
    <tr>
        <td>4</td>
        <td>5</td>
        <td>6</td>
    </tr>
    <tr>
        <td>7</td>
        <td>8</td>
        <td>9</td>
    </tr>
</table>

CSS

table {
    width: 330px;
    font-size: 0;/* eliminate spaces between TRs/TDs */
}
tr {
    display: inline;
}
td {
    display: inline-block;
    width: 100px;
    height: 100px;
    background: green;
    border: 0;
    border: 3px solid white;
    font-size: 20pt;
    color: white;
    line-height: 100px;
    vertical-align: middle;
    text-align: center;
}

.dnd-highlight {
    width: 100px;
    height: 100px;
    border: 3px dashed red;
    background: none;
}

Javascript

$('table').sortable({
    items: 'td', 
    placeholder: 'dnd-highlight'
});

You’ll have to modify according to your need, but that’s the idea.

  • It wasn’t quite what I was looking for but I changed the structure of the project and it helped me a lot. Thank you :)

  • For nothing Henrique, what a pity that could not use draggable, but as I said I strongly recommend the sortable for these cases. With this example I decided to even change my sortable from one of my hahaha projects.

  • The answer is interesting but has a problem that I do not know how to solve to suit my situation, If I drag the [1] to the place of the [3], who takes the previous position of the [1] is the [2] and not the [3]... has how to solve with Sortable?

  • @Gabrielcarneiro you are wanting Sortable to act with the nearby Divs neh? I will have to analyze here man... Honestly, I don’t know how to talk to you like that. I hope I see.

  • 1

    this @Ndelavi , I was able to solve, see the example: https://codepen.io/jo-o-santos/pen/rorEoo

  • @Gabrielcarneiro very good guy, so what I was seeing here would only have like this, treating the events. I’m glad you got! =)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.