在mousemove事件javascript中获取光标坐标

时间:2014-08-30 07:18:31

标签: javascript

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

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

使用函数mDown(target)我可以获得点击鼠标按钮的点的坐标

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

我需要在mUp(mouseup的处理程序)和mMove(mousemove的处理程序)中获取游标的坐标 我怎么能这样做?

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

1 个答案:

答案 0 :(得分: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;
}