`
javaEEdevelop
  • 浏览: 865346 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

js拖动层(支持所有浏览器)

 
阅读更多

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;
 } 
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics