Determining mouse position in a window focus event

I connected the focus listener to the window (using the prototype syntax):

Event.observe( window, 'focus', focusCb.bindAsEventListener( this ) );

I want to determine the position of the mouse when focusing the window. Unfortunately, in my focusCb method I do not have access to pages, pages, client or client.

Using quirksmode code:

function doSomething(e) {
    var posx = 0;
    var posy = 0;
    if (!e) var e = window.event;
    if (e.pageX || e.pageY)     {
        posx = e.pageX;
        posy = e.pageY;
    }
    else if (e.clientX || e.clientY)    {
        posx = e.clientX + document.body.scrollLeft
            + document.documentElement.scrollLeft;
        posy = e.clientY + document.body.scrollTop
            + document.documentElement.scrollTop;
    }

    // posx and posy contain the mouse position relative to the document
    // Do something with this information
}

I always get 0, 0.

I thought the focal event would have mouse location information.

  • Why does focus information not have this information?
  • More importantly, how do I get the mouse position when focusing the window?
+3
source share
4 answers

IE clientX clientY ; .

, . . . , .

, , script. .

+1

, , mousemove , - :

var x = 0, y = 0, window_got_focus = false;
window.onfocus = function () { window_got_focus = true };
document.onmousemove = function (event) {
  if (window_got_focus) {
    window_got_focus = false;
    x = event.clientX;
    y = event.clentY;
  }
};

, , , , .

+1

onclick .

EDIT: , , pointerX pointerY .

0
source

call to doSomething () function when moving the mouse

document.onmousemove = function (e) 
{
  doSomething(e);
};
0
source

Source: https://habr.com/ru/post/1722267/


All Articles