用鼠标左键按下onMouseOver(事件)

时间:2015-11-13 12:36:12

标签: javascript

在javascript中我有事件,例如

<div id="one" onMouseOver="func()">

当有人将鼠标悬停在我的div上时,我想调用一个函数,但只有当按下鼠标左键并且我想在用户释放按钮时调用另一个函数时。

1 个答案:

答案 0 :(得分:1)

首先,跟踪鼠标按下状态

window.mouseDown = false;
document.onmousedown = function() {
    window.mouseDown = true;
}
document.onmouseup = function() {
    window.mouseDown = false;
}

然后在func()

func = function () {
    if (window.mouseDown) {
        // the mouse is down and hovering div
    }
}

假设当用户在鼠标悬停时释放鼠标时需要触发事件:

<div id="one" onMouseOver="func();" onMouseOut="func2();">

window.hovering = false;

func = function () {
    window.hovering = true;
    if (window.mouseDown) {
        // the mouse is down and hovering div
    }
}

func2 = function () {
    window.hovering = false;
}

document.onmouseup = function() {
    window.mouseDown = false;
    if (window.hovering) {
        // user released mouse while hovering div
    }
}