当分配时,mousemove事件是否会触发

时间:2016-05-28 17:49:35

标签: javascript javascript-events mousemove

当用户按下按钮时,我需要使用光标位置设置初始状态,然后在光标移动时更新该状态。奇怪的是,it seems difficult to get the cursor position outside an event

我有以下代码:

    // Here is where I would like to set an initial state but don't know how to get the coordinates

    thing.addEventListener('mousemove', function (e) {
        // Here is where I update based on new e.clientX and e.clientY
    });

以上实际上似乎没有设置初始状态。似乎mousemove事件在我分配后立即触发,即使它自点击后可能没有移动。

有人知道这是否是我可以依赖的故意行为?

1 个答案:

答案 0 :(得分:0)

根据我在Windows 7上的测试,在两种情况下点击按钮时不会自动触发mousemove事件:

  1. 在Firefox上,如本文所述:What to do if "mousemove" and "click" events fire simultaneously?

  2. 当按钮具有焦点并使用空格键按下时(在IE,Chrome和Firefox中)

  3. 正如你所说,将鼠标位置放在事件处理程序之外,特别是在任何实际鼠标操作之前,这似乎是一项不可能完成的任务。