3

I am using HTML5 native drag and drop.

I am having problems finding the position of the draggable element relative to it's parent container.

Summary of Task

I have a "position: relative" div which I consider a 'droppable area'. Inside this div, I have several "position: absolute" image elements which I have marked as draggable. I want these images to be freely movable by the user.

To set the position of the element I've a listener on the 'dragend'.

The end top / left variables must be percentage based as opposed to pixel.

Current Attempt

Below you can see my attempt:

draggable.addEventListener('dragend', function (event) {
event.preventDefault();

//Gets the position of the two elements relative to the viewport
var droppableBoundingRect = droppable.getBoundingClientRect();

//Establishes the percentage using the droppable height and width
var draggableXPercentage = ((event.clientX - droppableBoundingRect.left) / droppable.clientWidth) * 100;
var draggableYPercentage = ((event.clientY - droppableBoundingRect.top) / droppable.clientHeight) * 100;

//Set the positional elements of the draggable element
event.target.style.top = draggableYPercentage + "%";
event.target.style.left = draggableXPercentage + "%";
}, false);

And here's a JSFiddle of what I was hoping to work, with comments showing what I was expecting each segment to do:

https://jsfiddle.net/oL8yd9Lr/

Perhaps I'm looking up the wrong tree, in the wrong forest. I'd appreciate any guidance.

David Moores
  • 1,025
  • 2
  • 9
  • 23

1 Answers1

0

Found a possible solution. Its not perfect but should illustrate how you can get the relative position. I may have gone a little overboard but I've commented the important parts.

Fiddle

;
(function($, undefined) {
  var dragging;

  $(function() {
    $('.dropzone').on({
      'dragover dragenter': dragover,
      'drop': drop
    }).on({
      'dragstart': dragstart,
      'dragend': dragend
    }, '.drag');
  });

  function dragstart(e) {
    e.stopPropagation();
    var dt = e.originalEvent.dataTransfer;
    if (dt) {
      dt.effectAllowed = 'move';
      dt.setData('text/html', '');
      dragging = $(this);
      // Set the position of the mouse relative to the element at 0,0
      dt.setDragImage(dragging.get(0), 0, 0);
    }
  }

  function dragover(e) {
    e.stopPropagation();
    e.preventDefault();
    var dt = e.originalEvent.dataTransfer;
    if (dt && dragging) {
      dt.dropEffect = 'move';
      dragging.hide(); // Hide the element while dragging
    }
    return false;
  }

  function drop(e) {
    e.stopPropagation();
    e.preventDefault();
    if (dragging) {
      var dropzone = $(this);
      // Get the offset of the dropzone relative to the window
      var offset = dropzone.offset();
      // Set the offset of the drag relative to the dropzone
      dragging.css({
        'top': e.clientY - offset.top,
        'left': e.clientX - offset.left
      });
      dragging.trigger('dragend'); // Trigger the dragend
    }
    return false;
  }

  function dragend(e) {
    if (dragging) {
      dragging.show();
      dragging = undefined;
    }
  }
}(jQuery));
malinkody
  • 529
  • 4
  • 11