在Dom事件中QQ拖拽源代码问题

来源:4-1 [DOM事件] QQ面板拖拽效果(上)

过客彳亍

2017-10-08 16:11

在Dom事件中QQ拖拽源代码问题:

请问各位大神:onmouseup放在onmousedown中和放在onmousedown外有什么区别么? 我先放在onmousedown中,按下几次释放后都没有什么问题,可是如果onmouseup放在onmousedown外面,当第二次按下时松开鼠标窗口还是跟着鼠标走,此时貌似onmouseup没有效用?想问下这种情况是为什么,谢谢!

写回答 关注

3回答

  • FloatingShuYin
    2017-10-10 12:13:55
    已采纳

    function getByClass(clsName,parent){
      var oParent=parent?document.getElementById(parent):document,
          eles=[],
          elements=oParent.getElementsByTagName('*');

      for(var i=0,l=elements.length;i<l;i++){
        if(elements[i].className==clsName){
          eles.push(elements[i]);
        }
      }
      return eles;
    }

    //这里定义了getByClass()

    var oTitle=getByClass('login_logo_webqq','loginPanel')[0];
       //这里调用了上面的getByClass(),目的是为了获取指定父族元素下指定类名的元素

       oTitle.onmousedown=fnDown;

    //这里是给上面获取的元素添加鼠标点击事件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;

    //最后你的问题,你说为什么不能把这句放在外面fnDown函数外面吗?

    //因为这是预先注册了鼠标点击事件,而.onmouseup是触发了鼠标点击事件调用了fnDown函数现场现注册的。

    //而如果你把    document.onmouseup=function(){...}放在外面的话 document就不是oTitle了,所以oTitle就没//有鼠标释放事件了,所以就你的鼠标一直没松开,所以你的鼠标一直在点击,所以你的鼠标移动就一直在拖动div,所以你懂了吗?
         

    }


    过客彳亍

    非常感谢!谢谢

    2017-11-26 22:17:36

    共 2 条回复 >

  • FloatingShuYin
    2017-11-20 13:24:44

    不要过于强迫症了哈,

  • 过客彳亍
    2017-10-10 13:06:49

    谢谢您的回答!我稍微理解了您的意思。

    但是当我把fnDown函数中的onmouseup事件去除,之后在oTitle.onmousedown=fnDown;后面加上

         oTitle.onmousedown=fnDown;

        //alert(document);

        oTitle.onmouseup=function(){

            //alert(document);

            // alert(oTitle);

            // this.onmousedown=null;

            this.onmousemove=null;

            this.onmouseup=null;

    之后拖拽功能还是不能实现,您说“document就不是oTitle”,可是现在我是在oTitle上面+的onmouseup事件,拖拽功能还是实现不了,不明白。(我就是想着onmousedown和onmouseup是一对,看着onmouseup在onmousedown里面怪难受,看看能不能将onmouseup放在外面)


    Floati...

    最近没有登慕课,迟来的回答! 也许你已经明白了,也许没有,我还是回答以下把。 onmousedown 是什么? 是鼠标按下! onmouseup 是什么? 是鼠标松开! 请注意,你的鼠标默认就是松开的,所以如果不把onmouseup事件放在onmousedown事件里面,那么onmouseup事件将一直处入触发状态!这显然不符合逻辑,符合逻辑的是我按下鼠标然后触发onmousedown事件,**按下鼠标后松开** 才能够触发onmouseup事件。

    2017-11-20 13:23:38

    共 1 条回复 >

DOM事件探秘

DOM事件?本课程会通过实例来给小伙伴们讲解如何使用这些事件

99532 学习 · 1298 问题

查看课程

相似问题