好的,这是一种方法。基本上,这个想法是在一个可拖动对象被拖到它上面时读取一个可放置对象的偏移量、宽度和高度。当一个元素被拖动而另一个元素被悬停时,重叠是使用鼠标位置、偏移量和元素的尺寸计算的(从鼠标位置中减去偏移量并与可放置的尺寸进行比较):
$(function() {
var offx, offy, w, h,
isOverEl = false;
$( ".component" ).draggable({
drag: function(event, ui) {
if(!isOverEl) return;
console.log(
((event.pageY-offy)/h)*100, // returns vertical overlap in %
((event.pageX-offx)/w)*100 //returns horizontal overlap in %
)
}
});
$( ".component" ).droppable({
over: function(event, ui) {
var $this = $(this),
offset = $this.offset();
offx = offset.left;
offy = offset.top;
w = $this.outerWidth();
h = $this.outerHeight();
isOverEl = true;
},
out: function(event, ui) {
isOverEl = false;
}
});
});