JSFiddle - React, Tailwind, and code Playground

by Natcher86

HTML

<div id="board">
    <div class="object">
    </div>
</div>
<div id="scene">
</div>

CSS

#board {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 200px;
    height: 200px;
    margin: -100px 0 0 -100px;
    background-color: green;
    z-index: 2;
}

#board.active {
    z-index: 0;
}

#scene {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 200px;
    height: 200px;
    margin: -100px 0 0 -100px;
    background-color: transparent;
    z-index: 1;
}

.object {
    position: absolute;
    left: 50px;
    top: 50px;
    width: 50px;
    height: 50px;
    background-color: blue;
}

JavaScript

var object = document.getElementsByClassName('object'),
    scene = document.getElementById('scene'),
    board = document.getElementById('board'),
    width = $('scene').width(),
    height = $('scene').height();

function initMove() {

  for(var i=0; i<object.length; i++) { 
      object[i].addEventListener("mousedown", function(event) {
        mouseDown = true;
        grabbed = $(this);
        objWidth = $(this).width();
        objHeight = $(this).height();
        startX = objWidth+event.offsetX;
        startY = objHeight+event.offsetY;
        objectX = parseFloat(grabbed.css('left'));
        objectY = parseFloat(grabbed.css('top'));
        console.log(grabbed);
        $("#board").addClass("active");
        //event.stopPropagation();
      }, false);

      object[i].addEventListener("mousemove", function(event) {
        event.stopPropagation();
        return false;
      }, false);
    
  }
    
  board.addEventListener("mousedown", function (event) {
    event.stopPropagation();
    $("#board").addClass("active");
  }, false); 
    

  scene.addEventListener("mousedown", function(event) {
    mouseDown = true;
    startX = width+event.offsetX;
    startY = height+event.offsetY;
    //event.stopPropagation();
  }, false);

  scene.addEventListener("mousemove", function (event) {
    //event.stopPropagation();
    width = $('scene').width();
    height = $('scene').height();
    moveX = width+event.offsetX;
    moveY = height+event.offsetY;
    console.log(moveX + ' + ' + moveY);
    if (mouseDown == true) {
      moveX = width+event.offsetX;
      moveY = height+event.offsetY;
      diffX = (moveX - startX);
      diffY = (moveY - startY);
      dragX = objectX + diffX;
      dragY = objectY + diffY;
      dragPiece(grabbed);
      event.stopPropagation();
    }
  }, false);

  document.addEventListener("mouseup", function(event) {
    mouseDown = false;
    $("#board").removeClass("active");
    grabbed.css('left', dragX + 'px');
   ...