JSFiddle - React, Tailwind, and code Playground

by oscard

HTML

<div id="wrapper">
    <div id="drag_wrap">
        <div id="draggable-element">Drag me!</div>
        <div class="text-field" contenteditable="true" id="text-filed">         
        </div>
        <div id="resize-handler" class="resize-handler"></div>
    </div>
</div>

CSS

body {padding:10px}
#wrapper{
    margin:5px;
    padding:10px;
    border:0px solid #ccc;
    position:absolute;
    bottom:0;
    left:0;
    right:0;
    top:0;
}
#drag_wrap {
  width:250px;
  border:1px solid #ccc;
  position:absolute;
}
#draggable-element {
  min-height:30px;
  color:white;
  padding:10px 12px;
  cursor:move;
  background:#ccc;
}
.text-field{
    min-height:80px;
    padding:5px;
    outline:none;
    border:1px dashed #ccc;
    margin:5px;
}
.resize-handler{
    height:15px;
    width:15px;
    position:absolute;
    right:-17px;
    bottom:-17px;
    border:1px solid #ccc;
    cursor:se-resize;
    display:none;
}

JavaScript

/***************************
****    Drag Element    ****
***************************/

var selected = null, // Object of the element to be moved
    x_pos = 0, y_pos = 0, // Stores x & y coordinates of the mouse pointer
    x_elem = 0, y_elem = 0; // Stores top, left values (edge) of the element

// Will be called when user starts dragging an element
function _drag_init(elem) {
    // Store the object of the element which needs to be moved
    selected = elem;
    x_elem = x_pos - selected.parentNode.offsetLeft;
    y_elem = y_pos - selected.parentNode.offsetTop;
}

// Will be called when user dragging an element
function _move_elem(e) {
    x_pos = document.all ? window.event.clientX : e.pageX;
    y_pos = document.all ? window.event.clientY : e.pageY;
    if (selected !== null) {
        selected.parentNode.style.left = (x_pos - x_elem) + 'px';
        selected.parentNode.style.top = (y_pos - y_elem) + 'px';
    }
}

// Destroy the object when we are done
function _destroy() {
    selected = null;
    return false;
}

// Bind the functions...
document.getElementById('draggable-element').onmousedown = function () {
    _drag_init(this);
    return false;
};

document.onmousemove = _move_elem;
document.onmouseup = _destroy;
//window.onmouseout = _destroy;
//document.getElementById('wrapper').onmouseout = _destroy;