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拖动层(支持所有浏览器)
本文将详细解析JavaScript中如何实现元素的拖拽操作,包括事件监听、位置更新及释放捕获等关键步骤。

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



