JSFiddle - React, Tailwind, and code Playground
by Tostifrosti
HTML
<html>
<head>
<script src="https://unpkg.com/interactjs@1.10.8/dist/interact.min.js"></script>
<script src="./assets/index-interactjs.js"></script>
<link href="./assets/index.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div id="dropzone" class="dropzone">
<p> Dropzone </p>
</div>
<div class="product-list">
<div class="draggable-item red">
<p> Draggable item </p>
</div>
<div class="draggable-item blue">
<p> Draggable item </p>
</div>
<div class="draggable-item red">
<p> Draggable item </p>
</div>
<div class="draggable-item blue">
<p> Draggable item </p>
</div>
<div class="draggable-item red">
<p> Draggable item </p>
</div>
</div>
</div>
</body>
</html>
CSS
#dropzone {
position: relative;
display: block;
width: 100%;
height: 300px;
background-color: white;
border: 1px solid black;
text-align: center;
font-size: 24px;
}
.product-list {
display: flex;
width: 600px;
height: 600px;
margin: 0 auto;
justify-content: space-between;
align-items: flex-start;
margin-top: 100px;
border: 1px solid blue;
flex-wrap: wrap;
}
.draggable-item {
width: 150px;
height: 150px;
color: white;
margin: 10px;
border-radius: 0.75em;
-webkit-transform: translate(0px, 0px);
transform: translate(0px, 0px);
text-align: center;
user-select: none;
touch-action: manipulation;
}
.dragging-element-active {
user-select: none; /* disable text selection */
touch-action: none; /* disable browser from panning when dragging */
}
.red {
background-color: red;
}
.blue {
background-color: blue;
}
JavaScript
document.addEventListener('DOMContentLoaded', function() {
var dragElement = null,
dragStartRect = { x: 0, y: 0, width: 0, height: 0 };
initDraggable();
// -- init --
function initDraggable() {
if (typeof window.interact === 'undefined' || window.interact === null) {
console.error('Could not initialize drag-drop! InteractJS not found.');
return;
}
interact('.draggable-item')
.styleCursor(true)
.draggable({
manualStart: true,
inertia: false,
autoScroll: false,
hold: 100, // start dragging after the pointer is held down
onstart: function(aEvent) {
onItemDragStart(aEvent);
},
onmove: function(aEvent) {
onItemDragMove(aEvent);
},
onend: function(aEvent) {
onItemDragEnd(aEvent);
}
})
.on('contextmenu', function(aEvent) {
aEvent.preventOriginalDefault();
})
.on('move hold', function(aEvent) {
var interaction = aEvent.interaction;
// don't try to start if already interacting
if (interaction.interacting()) {
return;
}
// if the pointer is a mouse and it wasn't a move event or if it's not a mouse (touch/pen) and it's not a hold event.
if ((aEvent.pointerType === 'mouse' && aEvent.type !== 'move') ||
(aEvent.pointerType !== 'mouse' && aEvent.type !== 'hold')) {
return;
}
interaction.start(
{ name: 'drag' },
aEvent.interactable,
aEvent.currentTarget
);
});
}
// -- Dragging ---
function...