function dragHandlerFun(obj){
var img=document.getElementById(obj);
var ox,oy,ex,ey,isDrag=false,isIE=!!document.all;
img.onmousedown=function(e){
e=e||event;
isDrag=true;
if(isIE){img.setCapture();}
ox=parseInt(img.style.left);
oy=parseFloat(img.style.top);
ex=e.clientX;
ey=e.clientY;
return false;//注意这里要return false,要不Firefox下拖拽不了。
}
document.onmousemove=function(e){
e=e||event;
if(isDrag===true){
var left=ox+e.clientX-ex+'px';
var top=oy+e.clientY-ey+'px';
//document.getElementById('txt').value=ox;
img.style.left=left;
img.style.top=top;
}
}
document.onmouseup=function(){
if(isIE)img.releaseCapture();
isDrag=false;
}
}
js拖动层(支持所有浏览器)
最新推荐文章于 2022-12-13 22:00:00 发布
本文详细介绍了如何使用JavaScript实现元素的拖拽功能,包括事件监听、坐标转换和释放捕捉等关键步骤,确保在不同浏览器环境下都能正常工作。
157

被折叠的 条评论
为什么被折叠?



