如何将拖拉事件跟点击事件分离?
需要做到:拖拉时不触动点击事件
var Drag={ obj:null, init:function(handle, dragBody, e){ if (e == null) { handle.onmousedown=Drag.start; } handle.root = dragBody; if(isNaN(parseInt(handle.root.style.left)))handle.root.style.left=0px; if(isNaN(parseInt(handle.root.style.top)))handle.root.style.top=0px;//确保后来能够取得top值 handle.root.onDragStart=new Function(); handle.root.onDragEnd=new Function(); handle.root.onDrag=new Function(); if (e !=null) { var handle=Drag.obj=handle; e=Drag.fixe(e); var top=parseInt(handle.root.style.top); var left=parseInt(handle.root.style.left); handle.root.onDragStart(left,top,e.pageX,e.pageY); handle.lastMouseX=e.pageX; handle.lastMouseY=e.pageY; document.onmousemove=Drag.drag; document.onmouseup=Drag.end; } }, start:function(e){ var handle=Drag.obj=this; e=Drag.fixEvent(e); var top=parseInt(handle.root.style.top); var left=parseInt(handle.root.style.left); //alert(left) //一般情况下 left top 在初始的时候都为0 handle.root.onDragStart(left,top,e.pageX,e.pageY); handle.lastMouseX=e.pageX; handle.lastMouseY=e.pageY; document.onmousemove=Drag.drag; document.onmouseup=Drag.end; return false; }, drag:function(e){//这里的this为document 所以拖动对象只能保存在Drag.obj里 e=Drag.fixEvent(e); var handle=Drag.obj; var mouseY=e.pageY; var mouseX=e.pageX; var top=parseInt(handle.root.style.top); var left=parseInt(handle.root.style.left);//这里的top和left是handle.root距离 不知如何应用进来。后来想了另一个方法,就是添加一个公共变量,在onmousedown、onmouseup、onclick分别获取鼠标的坐标,并记录在公共变量里,做了个小例子区分他们执行的顺序,如下:
xxxxxxxxxxxxxxxxxxxxxxxx
发现执行的顺序为onmousedown、onmouseup、onclick
原位置点击:
mousedown mouse.html:20 1:169 mouse.html:22 mouseup mouse.html:25 2:169 mouse.html:27 click mouse.html:15 3:169
拖动点击:
mousedown mouse.html:20 1:360 mouse.html:22 mouseup mouse.html:25 2:473 mouse.html:27 click mouse.html:15 3:473
上面可以发现,拖动点击的mousedown后移动,mouseup与click事件鼠标坐标发生变化,且一样。
故而,可以判断鼠标的坐标来区分是拖动点击还是原地点击~
当然这个是个土的办法,如果有更好的请回复~