简体   繁体   English

拖放时获取顶部 DIV 的 ID

[英]Get ID of top DIV when dragging & dropping

I need to get the ID of the top DIV when dragging and dropping another DIV onto it.将另一个 DIV 拖放到上面时,我需要获取顶部 DIV 的 ID。 The issue occurs when there is more than one DIV on top of another.当多个 DIV 位于另一个 DIV 之上时,就会出现此问题。 The bottom DIV, rather than the top, is always returned.始终返回底部 DIV,而不是顶部。

I've tried using event.target.id and elementFromPoint() and both return the bottom DIV (I've included both in the code below).我试过使用 event.target.id 和 elementFromPoint() 并且都返回底部 DIV(我已将两者都包含在下面的代码中)。

Fiddle: https://jsfiddle.net/wibbleface/t33ej6rn/小提琴: https : //jsfiddle.net/wibbleface/t33ej6rn/

 $(function() { $("#box1, #box2").draggable(); $("#box2, #box3").droppable({ drop: function(event, ui) { var dropPosition = $(this).position(); var elem = document.elementFromPoint(dropPosition.left, dropPosition.top).id; var target_id = event.target.id; alert("Dropped onto #" + elem + " / #" + target_id + " Left: " + dropPosition.left + " Top: " + dropPosition.top); } }); });
 #box1, #box2, #box3 { position: static; } #box1 { width: 220px; height: 220px; background-color: #fdd; } #box2 { width: 200px; height: 200px; background-color: #dfd; } #box3 { width: 240px; height: 240px; background-color: #ddf; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" type="application/javascript"></script> <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js" type="application/javascript"></script> <p>Drag #box1 onto #box3. An alert will show that it's been dropped on #box3. <br>Next, drag #box2 onto #box1. The alert will show #box3. <br>I want it to be able to show the box immediately below it (ie #box2).</p> <div id="box1">#box1 <br>First: drag me to #box3</div> <div id="box2">#box2 <br>Second: drag me to #box1, which is on #box3.</div> <div id="box3">#box3</div>

You need to get the coordinates of the draggable element rather than the position of the droppable element.您需要获取可拖动元素的坐标而不是可放置元素的位置。

In your example, you are using $(this).position() , where this refers to the element in which you are dropping the draggable element.在您的示例中,您使用的是$(this).position() ,其中this指的是放置可拖动元素的元素。 You can get a reference to the current draggable element with event.toElement , therefore you can use $(event.toElement).position() to retrieve the position of the draggable element.您可以使用event.toElement获取对当前可拖动元素的event.toElement ,因此您可以使用$(event.toElement).position()来检索可拖动元素的位置。 That will resolve the first issue.这将解决第一个问题。

Since elementFromPoint will return the top-most element, it will return the element that you are currently dragging since you are passing the coordinates of the current element that you are dragging.由于elementFromPoint将返回最顶部的元素,因此它将返回您当前正在拖动的元素,因为您正在传递您正在拖动的当前元素的坐标。 In order to ignore the element that you are dragging, you could simply add pointer-events: none to the element while it is in the drag state.为了忽略您正在拖动的元素,您可以简单地向处于拖动状态的元素添加pointer-events: none You can do this by applying the CSS to the class .ui-draggable-dragging which is only applied while the element is being dragged.您可以通过将 CSS 应用于.ui-draggable-dragging类来实现,该类仅在元素被拖动时应用。

Updated Example更新示例

$("#box1, #box2").draggable();
$("#box2, #box3").droppable({
  drop: function(event, ui) {
    var position = $(event.toElement).position();
    var element = document.elementFromPoint(
      position.left,
      position.top
    );

    if (element) {
      alert(element.id);
    }
  }
});
.ui-draggable-dragging {
  pointer-events: none;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM