JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdn.jsdelivr.net/interact.js/1.2.4/interact.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<div id="drag-container">
<div id="form-container">
<p class="drag-element-source drag-element">
<img src="https://i.imgur.com/JpSL8dF.jpg" draggable="false"/>
<a class="handle" href="#">
<i class="i fa fa-arrows"></i>
</a>
</p>
</div>
<div id="drop-container"></div>
</div>
CSS
img {
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-o-user-select: none;
user-select: none;
width: 120px;
display: block;
}
p {
margin: 0px;
}
#drag-container {
border: 2px dashed #0000ff;
margin: 0 auto;
padding: 50px;
}
#form-container {
position: relative;
border: 1px solid #0000ff;
height: 198px;
}
.drag-element {
position: absolute;
top: 0px;
left: 0px;
background-color: #fff;
border: 1px solid #ff0000;
}
.drag-element.dropped {
border: 1px solid #00ff00;
}
.drag-element-source {
position: relative;
float: left;
display: block;
margin-right: 6px;
}
.handle {
position: absolute;
top: 10px;
right: 20px;
width: 20px;
height: 20px;
color: white;
text-shadow: 0 0 2px black;
font-size: 2em;
}
#drop-container {
position: relative;
border: 2px dashed #ff0000;
height: 198px;
margin-top: 1em;
}
#drop-container.drop-target {
border: 2px dashed #00ff00;
}
JavaScript
interact('.drag-element').draggable({
'manualStart' : true,
'onmove' : function (event) {
var target = event.target;
var x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx;
var y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;
// translate the element
target.style.webkitTransform = target.style.transform = 'translate(' + x + 'px, ' + y + 'px)';
// update the posiion attributes
target.setAttribute('data-x', x);
target.setAttribute('data-y', y);
},
'onend' : function (event) {
console.log('Draggable: ', event);
}
}).on('move', function (event) {
var interaction = event.interaction;
// if the pointer was moved while being held down
// and an interaction hasn't started yet
if (interaction.pointerIsDown && !interaction.interacting() && event.currentTarget.classList.contains('drag-element-source')) {
var original = event.currentTarget;
// create a clone of the currentTarget element
var clone = event.currentTarget.cloneNode(true);
// Remove CSS class using JS only (not jQuery or jQLite) - http://stackoverflow.com/a/2155786/4972844
clone.className = clone.className.replace(/\bdrag-element-source\b/,'');
// insert the clone to the page
// TODO: position the clone appropriately
document.getElementById('form-container').appendChild(clone);
// start a drag interaction targeting the clone
interaction.start({ name: 'drag' }, event.interactable, clone);
} else {
interaction.start({ name: 'drag' }, event.interactable, event.currentTarget);
}
});
// enable draggables to be dropped into this
interact('#drop-container').dropzone({
// only accept elements matching this CSS selector
accept: '.drag-element',
// Require a 75% element overlap for a drop to be possible
...