1
I decided to create an application WEB using user interaction using API Drag And Drop (Drag and drop) of Javascript.
<html>
<head>
  <meta charset="UTF-8">
  <style>
      body {
        margin: 0;
      }
      * {
        margin: 0;
      }
  
      #drag {
        width: 100px;
        height: 100px;
        background: rgb(110, 107, 107);
        position: relative;
      }
      #drop {
        width: 200px;
        height: 200px;
        background: rgb(177, 174, 174);
        position: absolute;
        top: 0;
        left: 200px;
      }
      .paragraph {
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translateX(-50%) translateY(-50%);
        font-family: Arial, Helvetica, sans-serif;
        font-size: 20px;
        color: rgb(255, 255, 255);
      }
  </style>
</head>
<body>
<div id="drag" draggable="true"><p class="paragraph">Arraste</p></div>
<div id="drop"><p class="paragraph">Solte</p></div>
<script>
    let drag = window.document.querySelector("#drag");
    let drop = window.document.querySelector("#drop");
    drag.addEventListener("dragstart", function (event)
    {
      event.dataTransfer.setData("text", event.target.id);
    });
    drop.addEventListener("dragover", function (event)
    {
      event.preventDefault();
      drop.style.border = "5px dotted rgb(120, 120, 120)";
    });
    drop.addEventListener("dragleave", function (event)
    {
      drop.style.border = "none";
    });
    drop.addEventListener("drop", function (event)
    {
      event.preventDefault();
      let data = event.dataTransfer.getData("text");
      event.target.appendChild(window.document.getElementById(data));
      drop.style.border = "none";
    });
</script>
</body>
</html>This code on top is just a small piece of the application that I’m creating, I just put in the necessary code so the code doesn’t get too big, and you don’t waste time visualizing the entire code. But my problem is this: Drag And Drop from the above code works normally, but when you drag(drag) to div with id="drag"and drop(drop) her in div with id="drop" to div#drag stays inside the div#drop in a position left: 0 and top: 0 in relation to your parent container. What I would like is that at the time of dragging the div#drag and release her into the div#drop no matter where you’re dropping the div#drag she will remain in the position where you let go inside the div#drop. Here I quoted some sites that use the Drag And Drop in which the same effect I would like to happen with my application. Material Angular, Scratch.
Leandro, why don’t you use Jquery for this?
– Gleidson Henrique
Leandro, see if this link helps you. In it has a solution(using jquery) that does what you need: keeps the element where you drop.
– Guto Xavier
Thanks guys for the help, but is that I am focusing only on JS and would like a solution in just JS.
– Leandro Nascimento