JSFiddle - React, Tailwind, and code Playground

by wang lei

HTML

<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 dy = 0;
    var sy = 0;
    qwe.onmousemove = function(e){
        if (down) {
            var ev = e || event;
            rec.style.top = ev.clientY - (dy - sy) + 'px';
        }
    }
    rec.onmousedown = function(){
        dy = event.clientY;
        sy = parseInt(rec.style.top);
        if (!down) {
            down  = true;
        }
    }
    qwe.onmouseup = function(){
        if (down) {
            down = false;
        }
    }