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

to_be_or_not_to_be to_be_or_not_to_be 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
一个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;
};
};
}
...全文
评论 4
收藏 已收藏
分享
返回顶部
to_be_or_not_to_be
JavaScript 87,984 成员 224,681 内容