简体   繁体   English

如何找到可拖动元素相对于可放置区域的位置

[英]How do I find the position of the draggable element relative to the droppable area

I am using HTML5 native drag and drop. 我正在使用HTML5本机拖放。

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'. 我有一个“位置:相对” div,我认为它是“可投放区域”。 Inside this div, I have several "position: absolute" image elements which I have marked as draggable. 在该div内,我有几个“位置:绝对”图像元素,我将其标记为可拖动。 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'. 为了设置元素的位置,我在“ 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: 这是我希望工作的一个JSFiddle,其中的注释显示了我期望每个段的工作:

https://jsfiddle.net/oL8yd9Lr/ https://jsfiddle.net/oL8yd9Lr/

Perhaps I'm looking up the wrong tree, in the wrong forest. 也许我在错误的森林中寻找错误的树。 I'd appreciate any guidance. 我将不胜感激。

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));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将可拖动元素单独附加到多个可放置的div - How do I separately append a draggable element to multiple droppable divs 如果拖动到无效的droppable上,如何才能使draggable恢复到原始位置? - How do I get a draggable to revert to the original position only if it was dragged onto an invalid droppable? 追加可拖动元素相对于droppable元素的坐标 - Make coordinates of draggable element relative to droppable element after appending to it jQuery .data在可放置区域中的可拖动元素上保持未定义状态 - Jquery .data keeps going undefined on draggable element in droppable area 获取可拖动元素相对于元素(内部)的位置 - Getting position of draggable element relative to (inside of) element 如何使不同形状的画布可拖动,并将其特定区域拖放到同一画布中 - How can I make different shapes of a canvas draggable and particular area of it droppable in the same canvas 我如何水平居中一个元素和 position 其他元素相对于它? - How do I center an element horizontally and position other elements relative to it? 如何在draggable和droppable中的相对父级中选择类元素 - How to select class elements within a relative parent inside draggable and droppable 如何解决jQuery UI droppable bug,如果可拖动元素在可放置绑定之前被拖动,over / out不会触发 - How to workaround jQuery UI droppable bug where over/out do not fire if draggable element is dragging before droppable bound 如何在Jquery中的droppable上初始化可拖动对象? - How can I initialize a draggable on a droppable in Jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM