Running mousemove at current mouse position

Suppose we have a <div> with a mousemove handler associated with it. If the mouse pointer enters and moves around this div , the event is fired.

However, I am dealing with a rich web application where <div> moves around the screen, appears and disappears ... It may happen that <div> appears under the mouse pointer. In this case, mousemove does not start. However, I need it. (Note that replacing mousemove with mouseover does not change this behavior.)

In particular, the <div> should be highlighted, and I consider it a drawback of the interface in order to require the user to slightly move the mouse to bring up the highlight.

Is it possible to programmatically fire the mousemove event? And I do not mean

 document.getElementById('mydiv').onmousemove(); 

because onmousemove parameterized by an event object that I don't have.

Is it possible to make the browser behave as if onmousemove was launched in the current position of the mouse (although the mouse did not actually move)?

+4
source share
2 answers

In fact, you can create a mousemove event mousemove for transmission using something like this:

 window.onload = function () { document.getElementById("test").onmousemove = function(e) { console.log(e); }; document.getElementById("test").onclick = function(e) { var e = document.createEvent('MouseEvents'); e.initMouseEvent('mousemove',true,true,document.defaultView,<detail>,<screenX>,<screenY>,<mouseX>,<mouseY>,false,false,false,false,<button>,null); this.onmousemove(e); }; }; 

Of course, here I shoot on a click, but you can do it on any event that you want, for example, when your div becomes visible, check if the mouse is in it. You just need to make sure your settings are correct, and you need to track the mouse position yourself. Also, some differences in IE, I think. Here is my source: http://chamnapchhorn.blogspot.com/2008/06/artificial-mouse-events-in-javascript.html . He added some extra code to account for it.

There is a script for the game. http://jsfiddle.net/grimertop90/LxT7V/1/

+4
source

You can modify your mousemove to save a state variable with the current mouse coordinates and use this information to detect a collision that you cause when you move the mouse or move the div.

A small example of what might look like

+5
source

All Articles