繁体   English   中英

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

[英]Why does Chrome raise a mousemove on mousedown?

我注意到在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>

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>";
    }
}

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

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

有人知道解决方法吗?

谢谢戴夫

确实是一个奇怪的问题,但是有一点旗帜就可以规避这个问题: http//jsfiddle.net/3a28p7ek/

更改非常简单,在每个鼠标按下时将ignoreNextMove设置为true ,并在设置此标志后取消移动处理程序,重置标志以便正确处理常规移动事件:

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);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM