0

I need to setup an initial state using the cursor position when the user presses a button, then update that state when the cursor moves. Curiously, it seems difficult to get the cursor position outside an event.

I have the following code:

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

The above actually seems to work without even setting the initial state. It seems the mousemove event is fired as soon as I assigned it, even though it perhaps has not moved since the click.

Does anybody know if this is intentional behavior that I can rely on?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Houston Fortney
  • 109
  • 1
  • 2

1 Answers1

0

According to my tests on Windows 7, the mousemove event is not triggered automatically on the button click in two situations:

  1. On Firefox, as mentioned in this post: What to do if "mousemove" and "click" events fire simultaneously?

  2. When the button has the focus and is pressed with the space bar (in IE, Chrome and Firefox)

As you say, getting the mouse position outside of an event handler, and especially before any actual mouse action, seems to be an impossible task.

Community
  • 1
  • 1
ConnorsFan
  • 70,558
  • 13
  • 122
  • 146