问答详情
源自:4-1 [DOM事件] QQ面板拖拽效果(上)

onmousedown 和onmouseup应该在哪一段代码中写?

看程序中,   oTitle.onmousedown=fnDown;  是作用在login_logo_webqq这个DIV的, 按道理, 那么onmouseup也应该是作用在这个地方,为什么下面document.onmouseup是作用在document里面的事件?  按道理, onmousedown和onmouseup是不是应该写在一个函数里面的? 因为不可能一直是onmousedown的状态啊

function drag()  {

    var oTitle=getclass("login_logo_webqq","loginPanel")[0];

    oTitle.onmousedown=fnDown;

}


function fnDown(event){

    event=event||window.event;

   var  oDrag=document.getElementById("loginPanel");

     disX=event.clientX-oDrag.offsetLeft,

     disY=event.clientY-oDrag.offsetTop;

      document.onmousemove=function(event) { 

        event=event||window.event;

        fnMove(event,disX,disY); 

      }


       document.onmouseup=function() {document.onmousemove=null;

       document.onmouseup=null;

       }

}


提问者:rainy_li3676598 2016-10-27 15:03

个回答

  • 慕粉5366927
    2016-10-29 10:19:55

    我看看试了课题里的代码,把document改成otitle,效果实现的是一样的,用document的话,可以不用再取一下那个otitle这变量,减少代码的量。