如何知道DIV移动到了哪个页面元素上方
一个dragdrop程序,监听mousedown/mousemove/mouseup事件
mousedown的时候,创建一个div,随着鼠标移动,如何在mousemove和mouseup事件中得知div移动到哪个页面元素上方了,基本代码如下:
window.onload=function(){
drag(document.getElementById('drag'),[200,400,30,30]);
};
function drag(o,r){
o.firstChild.onmousedown=function(){return false;};
o.onmousedown=function(a){
var d=document;if(!a)a=window.event;
var x=a.layerX?a.layerX:a.offsetX,y=a.layerY?a.layerY:a.offsetY;
var oo = document.createElement("DIV");
document.body.appendChild(oo);
oo.style.position = "absolute";
oo.style.border = "1px solid #000000";
if(oo.setCapture)
oo.setCapture();
else if(window.captureEvents)
window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
d.onmousemove=function(a){
if(!a)a=window.event;
if(!a.pageX)a.pageX=a.clientX;
if(!a.pageY)a.pageY=a.clientY;
var tx=a.pageX-x,ty=a.pageY-y;
oo.style.left=tx<r[0]?r[0]:tx>r[1]?r[1]:tx;
oo.style.top=ty<r[2]?r[2]:ty>r[3]?r[3]:ty;
};
d.onmouseup=function(){
if(oo.releaseCapture)
oo.releaseCapture();
else if(window.releaseEvents)
window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);
d.onmousemove=null;
d.onmouseup=null;
};
};
}