2014-08-30 107 views
-3

如何在mousemove和mouseup的处理程序中获取光标位置的坐标?在mousemove事件中获取光标坐标javascript

conDiv.addEventListener("mousedown", mDown, false); 
conDiv.addEventListener("mouseup", mUp, false); 
conDiv.addEventListener("mousemove", mMove, false); 

使用功能mDown(目标)我能得到点的坐标鼠标按钮被点击

function mDown(target) { 
    x1 = target.clientX; 
    y1 = target.clientY; 
    console.log("y1 ->> " + y1); 
    console.log("x1 ->> " + x1); 
} 

我需要得到光标的坐标MUP(鼠标松开的处理程序)和mMove(处理程序mousemove) 我该怎么做?

function mUp(tareget){ 
    x2 = target.clientX; // warning: target is not difined the same situation in nMove 
    y2 = target.clientY; 
} 

回答

1

你可以做到这一点是相同的,但你必须在你的函数一个错字:

function mUp(tareget){ // here use target instead of tareget!!! 
    x2 = target.clientX; // warning: target is not difined the same situation in nMove 
    y2 = target.clientY; 
} 
+1

是啊,我已经发现了这个错误。谢谢你的帮助)) – 2014-08-30 07:32:11

相关问题