测试jsFiddle
// function to determine, that rectangles intersect
var intersect = function(a,b){
return(
(
(
( a.x >= b.x && a.x <= b.x1 ) || ( a.x1 >= b.x && a.x1 <= b.x1 )
) && (
( a.y >= b.y && a.y <= b.y1 ) || ( a.y1 >= b.y && a.y1 <= b.y1 )
)
) || (
(
( b.x >= a.x && b.x <= a.x1 ) || ( b.x1 >= a.x && b.x1 <= a.x1 )
) && (
( b.y >= a.y && b.y <= a.y1 ) || ( b.y1 >= a.y && b.y1 <= a.y1 )
)
)
) || (
(
(
( a.x >= b.x && a.x <= b.x1 ) || ( a.x1 >= b.x && a.x1 <= b.x1 )
) && (
( b.y >= a.y && b.y <= a.y1 ) || ( b.y1 >= a.y && b.y1 <= a.y1 )
)
) || (
(
( b.x >= a.x && b.x <= a.x1 ) || ( b.x1 >= a.x && b.x1 <= a.x1 )
) && (
( a.y >= b.y && a.y <= b.y1 ) || ( a.y1 >= b.y && a.y1 <= b.y1 )
)
)
);
}
// elements - elements to fire dragover event on draggable element hover
var coordinates = {},
elements = ['#two', '#three'];
$('#one').draggable({
start: dragStart,
cursor: 'move',
stop: dragStop,
create: function(event, ui){
for (var k in elements){
coordinates[k] = {
x: $(elements[k]).offset().left,
y: $(elements[k]).offset().top,
x1: $(elements[k]).offset().left + $(elements[k])[0].clientWidth,
y1: $(elements[k]).offset().top + $(elements[k])[0].clientHeight
}
}
},
drag: function(event, ui){
var draggableElementCoord = {
x: ui.offset.left,
y: ui.offset.top,
x1: ui.offset.left + ui.helper[0].clientWidth,
y1: ui.offset.top + ui.helper[0].clientHeight
};
for (var k in elements){
if (intersect(coordinates[k], draggableElementCoord)){
$(elements[k]).trigger('dragover');
}
}
}
});
使用:
$(document).on('dragover', '#two', function(e){
// your code
});