4

我想要鼠标处于Down 状态up 状态

document.onmousemove = mouseMove;
document.onmousedown = mouseDown;
document.onmouseup   = mouseUp;

function mouseMove(ev) {

    mouseState="";
    //How can I know the state button of mouse button from here 
    if(mouseState=='down') {
        console.log('mouse down state')
    }

    if(mouseState=='up')  {
        console.log('mouse up state')
    }
}

function mouseDown(ev) {
    console.log('Down State you can now start dragging');
    //do not write any code here in this function
}

function mouseUp(ev) {
    console.log('up state you cannot drag now because you are not holding your mouse')
    //do not write any code here in this function
} 

当我移动鼠标时,程序现在应该在控制台上显示所需的 mouseState 值

4

2 回答 2

3

你可以查一下MouseEvent.which楼盘。

function mouseMove(ev) {
    if(ev.which==1) {
        console.log('mouse down state with left click');
    } else if(ev.which==3)  {
        console.log('mouse down state with right click');
    } else {
        console.log('mouse update');
    } 
}
于 2012-08-31T05:53:42.940 回答
2

你只需要为它创建一个变量。

document.onmousemove = mouseMove;
document.onmousedown = mouseDown;
document.onmouseup   = mouseUp;
var mouseState = "up";

function mouseMove(ev) {

    //How can I know the state of mouse from here 
    if(mouseState=='down') {
        console.log('mouse down state')
    }

    if (mouseState=='up')  {
        console.log('mouse up state')
    }
}

function mouseDown(ev) {
    mouseState = "down";
    console.log('Down State you can now start dragging');
    //do not write any code here in this function
}

function mouseUp(ev) {
    mouseState = "up";
    console.log('up state you cannot drag now because you are not holding your mouse')
    //do not write any code here in this function
}

您应该通过将“mousemove”事件登录到控制台来查看它。那里可能有一个属性可以显示鼠标的状态,就像 keypress 事件有一个属性可以告诉您是否按下了 shift 按钮。但这可能不是跨浏览器兼容的。

于 2012-08-31T05:50:24.253 回答