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

为什么我的onmouseup不起作用的?

<script type="text/javascript">

window.onload=Drag;

function Drag(){

var oDrag=document.getElementById('one'),

w=document.getElementById('w');

document.onmousedown=fnDown;

}

function fnDown(event){

var oDrag=document.getElementById('one'),

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.onmousedown=null;

document.onmouseup=null;

}

}

function fnMove(event,disX,disY){

var oDrag=document.getElementById('one'),

l=event.clientX-disX,

t=event.clientY-disY;

oDrag.style.left=l+'px';

oDrag.style.top=t+'px';

}

</script>


提问者:GacentJohn 2015-07-18 17:49

个回答

  • 秃头waanjia
    2022-11-30 16:07:46

             // 鼠标弹起时不再移动
            document.onmouseup = function () {
              document.onmousemove = null;
              return false
            };
    
            // 解决有些时候,在鼠标松开的时候,元素仍然可以拖动;
            document.ondragstart = function (ev) {
              ev.preventDefault();
              console.log(1212)
            };
            document.ondragend = function (ev) {
              ev.preventDefault();
            };


  • 22不小了
    2015-10-13 22:39:43

    楼上的一样,事件设置为null了

  • 悟道参禅
    2015-09-08 17:16:15


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