如何知道DIV移动到了哪个页面元素上方

to_be_or_not_to_be 2006-08-04 12:20:00
一个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;
};
};
}
...全文
378 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
leohuang 2006-08-04
  • 打赏
  • 举报
回复
让元素mouseover的时候接收吧

----------------------------------------
var mouseDownFlag=false;//鼠标是否按下的标志

document.onmousedown=function(){
mouseDownFlag=true;//鼠标按下将标志置true
}

document.onmouseup=function(){
mouseDownFlag=false;//鼠标松开将标志置false
}

window.onload=function(){
drag(document.getElementById('drag'),[200,400,30,30]);


for(var i=0;i<document.body.childNodes.length;i++){
document.body.childNodes[i].onmouseover=function(){
if(mouseDownFlag){
alert("移过来了");
mouseDownFlag=false;
}
}
}


};
BlueDestiny 2006-08-04
  • 打赏
  • 举报
回复
用遍历的方法。举个例子:在table中,拖动互换元素

按下,onmousedown->出现浮动DIV
onmouseover->
遍历你table下所有cell的坐标,如果cell坐标在你现在浮动的DIV坐标范围内,则高亮,否则跳过
onmouseup->判断坐标在哪个cell的范围内,并swapNode或return;
leohuang 2006-08-04
  • 打赏
  • 举报
回复
你的意思是说这个DIV盖住了页面的哪个元素?

这就不好办了
to_be_or_not_to_be 2006-08-04
  • 打赏
  • 举报
回复
谢谢楼上的,不过不只是mouseup,mousemove我也想知道div到了哪个元素的上方了

87,997

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧