HTML5 draggable

In this example below you will see how to do a HTML5 draggable with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by teckliew, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright teckliew ©
  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>HTML5 draggable</title>
  
  
  
      <style>
      /* NOTE: The styles were added inline because Prefixfree needs access to your styles and they must be inlined if they are on local disk! */
      #box-container {
  max-width: 50%;
  min-width: 370px;
  margin-left: auto;
  margin-right: auto;
}

.box {
  width: 80px;
  height: 80px;
  padding: 10px;
  margin: 10px;
  border-radius: 10px;
  text-align: center;
  line-height: 80px;
  display: inline-block;
  background-color: lightgray;
}

    </style>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>

</head>

<body>

  <div id="box-container">
  <div class="box" draggable="true">
    Box 01
  </div>
  <div class="box" draggable="true">
    Box 02
  </div>
  <div class="box" draggable="true">
    Box 03
  </div>
  <div class="box" draggable="true">
    Box 04
  </div>
  <div class="box" draggable="true">
    Box 05
  </div>
  <div class="box" draggable="true">
    Box 06
  </div>
  <div class="box" draggable="true">
    Box 07
  </div>
  <div class="box" draggable="true">
    Box 08
  </div>
  <div class="box" draggable="true">
    Box 09
  </div>
  <div class="box" draggable="true">
    Box 10
  </div>
</div>
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/teckliew/html5-draggable-pEKWPK */
#box-container {
  max-width: 50%;
  min-width: 370px;
  margin-left: auto;
  margin-right: auto;
}

.box {
  width: 80px;
  height: 80px;
  padding: 10px;
  margin: 10px;
  border-radius: 10px;
  text-align: center;
  line-height: 80px;
  display: inline-block;
  background-color: lightgray;
}


/*Downloaded from https://www.codeseek.co/teckliew/html5-draggable-pEKWPK */
(function () {
  var source = null;
  var boxContainer = document.getElementById('box-container');

  boxContainer.addEventListener('dragstart', dragstartHandler, false);
  boxContainer.addEventListener('dragenter', dragenterHandler, false);
  boxContainer.addEventListener('dragleave', dragleaveHandler, false);
  boxContainer.addEventListener('dragover', dragoverHandler, false);
  boxContainer.addEventListener('dragend', dragendHandler, false);
  boxContainer.addEventListener('drop', dropHandler, false);

  function dragstartHandler(e) {
    var target = e.target;
    var dataTransfer = e.dataTransfer;

    target.style.opacity = '0.4';
    source = target;

    dataTransfer.effectAllowed = 'move';
    dataTransfer.setData('text/html', target.innerHTML.trim());
  }

  function dragenterHandler(e) {
    var target = e.target;

    if (target.id !== 'box-container') {
      target.style.color = 'white';
    }
  }

  function dragleaveHandler(e) {
    e.target.style.color = 'black';
  }

  function dragoverHandler(e) {
    e.preventDefault();
    e.dataTransfer.dropEffect = 'move';
    return false;
  }

  function dragendHandler(e) {
    var boxes = document.getElementsByClassName("box");
    [].forEach.call(boxes, function (box) {
      box.style.opacity = '1.0';
      box.style.color = 'black';
    });
  }

  function dropHandler(e) {
    e.stopPropagation;
    var target = e.target;

    if (source != target) {
      source.innerHTML = target.innerHTML.trim();
      target.innerHTML = e.dataTransfer.getData('text/html');
    }

    return false;
  }
})();

Comments