最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get element currently under mouse without using mouse events - Stack Overflow

programmeradmin3浏览0评论

WRT building a Firefox Add-on.

Is it possible to get the element under the mouse via some XPCOM or javascript method? (non-js-ctypes please as that requires OS specificity)

I want to detect what is under the mouse when user presses Ctrl + Shift + M.

Right now I'm adding a mouseover listener to the document when the user presses this hotkey, so I can get the element under the mouse when he moves it, but not the element that was under the mouse exactly when he pressed the hotkey bination.

WRT building a Firefox Add-on.

Is it possible to get the element under the mouse via some XPCOM or javascript method? (non-js-ctypes please as that requires OS specificity)

I want to detect what is under the mouse when user presses Ctrl + Shift + M.

Right now I'm adding a mouseover listener to the document when the user presses this hotkey, so I can get the element under the mouse when he moves it, but not the element that was under the mouse exactly when he pressed the hotkey bination.

Share Improve this question edited Jul 2, 2014 at 23:14 Noitidart 37.3k40 gold badges176 silver badges354 bronze badges asked Jul 2, 2014 at 18:28 BlagohBlagoh 1,2351 gold badge16 silver badges29 bronze badges 7
  • i would suggest having a listener on the mousemove event to track the mouse. then using the mouse's coordinates, find the element beneath it. this is annoying but possible. see more here: stackoverflow./questions/7790725/… – user428517 Commented Jul 2, 2014 at 18:30
  • The issue with elementFromPoint is I have to add mousemove and the element will not be available until user makes movement of mouse after pressing hotkey. I basically use the 2nd method in this solution: stackoverflow./a/4711224/3791822 – Blagoh Commented Jul 2, 2014 at 18:34
  • 1 listen on mousemove ALL the time, storing the position somewhere. then when the key bination is pressed, use the last recorded mouse position. – user428517 Commented Jul 2, 2014 at 18:36
  • Oh yes that's an idea, I had that too but forgot to mention it so up vote for that. A persistent mousemove is not so good for performance especially from the add-on scope so I was hoping to avoid that please. – Blagoh Commented Jul 2, 2014 at 18:43
  • then you're probably out of luck. are you sure it hurts performance that much? i've done things like this before with little issue. a simple handler that updates xy values in an array is not very taxing. that's all it would need to do. – user428517 Commented Jul 2, 2014 at 18:47
 |  Show 2 more ments

1 Answer 1

Reset to default 15

I just looked through the source for code that gets (or stores and makes available) the cursor position. I didn't find anything one could use (from Javascript, XPCOM or not). I might have missed something... MXR is your friend.

However, if you want to avoid mousemove (and this is a good idea in general), you can just look for the innermost hovered element, e.g. like so.

function getInnermostHovered() {
    var n = document.querySelector(":hover");
    var nn;
    while (n) {
        nn = n;
        n = nn.querySelector(":hover");
    }
    return nn;
}

(fiddle demoing the principle)

While this is what I'd consider a hack, it seems to work good enough most of the time, but will fail if the element has mouse events disabled via pointer-events. There could be other issues I didn't think of...

Of course, this can return nothing when the document has no hovered element (e.g. the mouse is not actually within the document).

发布评论

评论列表(0)

  1. 暂无评论