[英]How do I get the top most element at a given location with jQuery?
I have a draggable which on the stop event, I want move to a certain boundary IF it wasnt dragged onto anything else. 我有一个可拖动对象,在停止事件上,如果它没有拖动到其他任何对象上,我想移到某个边界。 Problem: the drop event handled by droppable occurs AFTER the stop event from the draggable handler. 问题:在可拖动处理程序的stop事件之后,发生了droppable处理的drop事件。 The stop handler for draggables gives the location that the draggable was dragged to, but I dont know how to determine what element is at that location. 可拖动对象的停止处理程序提供了可拖动对象被拖动到的位置,但是我不知道如何确定该位置上的元素。 Any ideas? 有任何想法吗?
You could probably just loop through the droppable elements and check their offset position. 您可能只需要遍历可放置元素并检查其偏移位置即可。 Combined with the width/height of each element, you can calculate which element the draggable was over when the stop event is fired. 结合每个元素的宽度/高度,您可以计算触发stop事件时可拖动对象在哪个元素上。 Something like this (this is untested, off the top of my head.) 这样的事情(未经测试,在我的头顶上)。
function stopCallback (x, y) {
var $droppables = $(".droppable"),
containedDraggableAtStop = function($item, left, top) {
var itemL = $item.offset().left,
itemT = $item.offset().top,
itemR = itemL + $item.width,
itemB = itemT + $item.height;
if (top > itemT && top < itemB && left > itemL && left < itemR) {
return true;
}
return false;
},
numDroppables = $droppables.length,
i;
// x, y are the coords passed from the stop function
for (i = 0; i < numDroppables; i++) {
if (containedDraggableAtStop($droppables.eq(i), x, y)) {
return $droppables.eq(i);
}
}
return -1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.