I have a picture that when I drag it to the red circle it copies. I want it so it stays where it is copied, not snap to the upper-left corner. How do I do this?
HTML:
<span class="box" ondrop="drop(event)" ondragover="allowDrop(event)"></span>
<br />
<div id="picture" ondrop="drop(event)">
<img src="https://www.w3schools.com/html/img_w3slogo.gif" draggable="true" ondragstart="drag(event)" id="drag1" width="88" height="31">
</div>
CSS:
.box {
display: block;
height: 300px;
width: 300px;
background-color: red;
}
JavaScript:
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
var nodeCopy = document.getElementById(data).cloneNode(true);
nodeCopy.id = "newId";
ev.target.appendChild(nodeCopy);
}
Or see a TryIt here: https://www.w3schools.com/code/tryit.asp?Filename=GBY2BVYBXUPW