为什么Chrome会在mousedown上移动鼠标?

Bad*_*pot 12 javascript google-chrome

我注意到在Chrome中(我使用的是Chrome 35.0.1916.114 [ 更新:也发生在"35.0.1916.153 m"],Windows 7 64位)当我单击左键时不仅会引发mouseDown事件(如我期待)还有一个mouseMove.

这个小提琴中,如果你单击输入元素,你会看到每个mouseDown事件的'D'和每个mouseMove的'M'.

HTML:

<input id="txt" type="text"/>
<p>Moves</p><p id="moves">0</p>
<p>Downs</p><p id="downs">0</p>
<p id="activity">Activity</p>
Run Code Online (Sandbox Code Playgroud)

JS:

$( "#txt" ).mousedown(function() {
     document.getElementById("activity").innerHTML +="D";
    update(false,true);
});
$( "#txt" ).mousemove(function() {
    document.getElementById("activity").innerHTML +="M";
    update(true,false);
});

function update(move, down) 
{
    var moves=document.getElementById("moves").innerHTML;
    if (move) 
    {
        moves ++;
        document.getElementById("moves").innerHTML=moves;
    }

    var downs=document.getElementById("downs").innerHTML;
    if (down) 
    {
        downs ++;
        document.getElementById("downs").innerHTML=downs;
    }
    var d=parseInt(downs);
    var m=parseInt(moves);
    if ((d+m)%25==0)
    {
        document.getElementById("activity").innerHTML +="<br>";
    }
}
Run Code Online (Sandbox Code Playgroud)

在FF和IE11中,一旦光标位于输入元素中,那么您将能够获得连续的'D'(即,单击会引发单个mouseDown事件).在Chrome中,每次鼠标单击都会引发mouseDown和两个mouseMove事件.

这不是由于我使用轨迹球时鼠标的任何轻微摆动,因此光标是绝对静止的.

有人知道解决方法吗?

谢谢戴夫

gar*_*arg 5

确实是一个奇怪的问题,但是可以稍加标记就可以绕开它:http : //jsfiddle.net/3a28p7ek/

更改非常简单,重置该标志后,将每只鼠标按下设置ignoreNextMovetrue,并取消移动处理程序(如果设置了此标志),以便正确处理常规的移动事件:

ignoreNextMove = false;

$( "#txt" ).mousedown(function() {
    ignoreNextMove = true;
    document.getElementById("activity").innerHTML +="D";    
    update(false,true);
});
$( "#txt" ).mousemove(function() {
        if(ignoreNextMove)
    {
        ignoreNextMove = false;
        return;
    }
    document.getElementById("activity").innerHTML +="M";
    update(true,false);
});
Run Code Online (Sandbox Code Playgroud)