JSFiddle - React, Tailwind, and code Playground

by wang lei

HTML

<!-- <div class="box">
  <div class="children" id="move"></div>  
</div> -->
<div id="box">
    <div id="children" style="position:absolute;left: 100px;top: 100px; width: 50px;height: 50px;background: red;"></div>
</div>

CSS

#box {
  width: 1000px;
  height: 600px;
  background: #ccc;
  /* margin: 50px 0 0 100px; */
}

JavaScript

var qwe = document.getElementById('box');
    var rec = document.getElementById("children")
    var down = false;
    var dx = 0;
    var dy = 0;
    var sx = 0;
    var sy = 0;
    qwe.onmousemove = function(e){
        if (down) {
            var ev = e || event;
console.log(ev.clientY)
            rec.style.top = ev.clientY - (dy - sy) + 'px';
            rec.style.left = ev.clientX - (dx - sx) + 'px';
        }
    }
    rec.onmousedown = function(){
        dx = event.clientX;
        dy = event.clientY;
        sx = parseInt(rec.style.left);
        console.log(event.x)
        sy = parseInt(rec.style.top);
        if (!down) {
            down  = true;
        }
    }
    qwe.onmouseup = function(){
        if (down) {
            down = false;
        }
    }