IE HTML5 Drag and Drop Solution

by Majid Hajian

HTML

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

<div id="drag-1" class="draggable">
  <p> You can drag one element </p>
</div>

<div class="bg draggable" draggable="true" ></div>
<img  class="draggable" draggable="true" src="http://demosthenes.info/assets/images/thumbnails/homer-simpson.svg">
</img>
<br/>
<img  class="draggable" draggable="true" src="http://www.newlafayette.org/wp-content/uploads/2010/11/BartSimpson.jpg" />


<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
  <symbol id="circle" viewBox="0 0 400 400">
    <circle cx="00" cy="200" r="200"/>
  </symbol>
</svg>
<div class="svg draggable" draggable="true"> <svg><use xlink:href="#circle" /></svg></div>

CSS

#drag-1{
  width: 25%;
  height: 100%;
  min-height: 6.5em;
  margin: 10%;

  background-color: #29e;
  color: white;

  border-radius: 0.75em;
  padding: 4%;

  -webkit-transform: translate(0px, 0px);
          transform: translate(0px, 0px);
}

#drag-me::before {
  content: "#" attr(id);
  font-weight: bold;
}

img {
  width: 150px;
  height: 150px;
}

div.bg {
  background-image: url("http://demosthenes.info/assets/images/thumbnails/homer-simpson.svg");
  background-size:  150px 150px;
  
  width: 150px;
  height: 150px;
}

div.svg {
  width: 150px;
  height: 150px;
  border: 1px solid red;
}

JavaScript

// target elements with the "draggable" class
interact('.draggable')
  .draggable({
    // enable inertial throwing
    inertia: true,
    // keep the element within the area of it's parent
    restrict: {
      restriction: "parent",
      endOnly: true,
      elementRect: { top: 0, left: 0, bottom: 1, right: 1 }
    },
    // enable autoScroll
    autoScroll: true,

    // call this function on every dragmove event
    onmove: dragMoveListener,
    // call this function on every dragend event
    onend: function (event) {
      var textEl = event.target.querySelector('p');

      textEl && (textEl.textContent =
        'moved a distance of '
        + (Math.sqrt(event.dx * event.dx +
                     event.dy * event.dy)|0) + 'px');
    }
  });

  function dragMoveListener (event) {
    var target = event.target,
        // keep the dragged position in the data-x/data-y attributes
        x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx,
        y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;

    // translate the element
    target.style.webkitTransform =
    target.style.transform =
      'translate(' + x + 'px, ' + y + 'px)';

    // update the posiion attributes
    target.setAttribute('data-x', x);
    target.setAttribute('data-y', y);
  }

  // this is used later in the resizing and gesture demos
  window.dragMoveListener = dragMoveListener;