这个问题已经在这里有一个答案:
- JavaScript的:检查是否按下鼠标按钮? 16个回答
我想在Down状态或上了状态的鼠标状态
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所需的值向上或向下控制台现在
你只需要创建一个变量它。
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
}
你应该看看在“鼠标移动”的情况下,通过登录入控制台。 有可能是一个属性那里显示鼠标的那种状态,就像按键事件有如果按下Shift键,它告诉你的属性。 但是,这可能不是跨浏览器兼容。
你可以检查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');
}
}