Skip to content

Instantly share code, notes, and snippets.

@callmephilip
Created August 29, 2012 19:39
Show Gist options
  • Save callmephilip/3517765 to your computer and use it in GitHub Desktop.
Save callmephilip/3517765 to your computer and use it in GitHub Desktop.
[JavaScript] Dispatching mouse move event
// look here for more details : https://developer.mozilla.org/en-US/docs/DOM/event.initMouseEvent
var mouseMoveEvent = document.createEvent("MouseEvents");
mouseMoveEvent.initMouseEvent(
"mousemove", //event type : click, mousedown, mouseup, mouseover, mousemove, mouseout.
true, //canBubble
false, //cancelable
window, //event's AbstractView : should be window
1, // detail : Event's mouse click count
50, // screenX
50, // screenY
50, // clientX
50, // clientY
false, // ctrlKey
false, // altKey
false, // shiftKey
false, // metaKey
0, // button : 0 = click, 1 = middle button, 2 = right button
null // relatedTarget : Only used with some event types (e.g. mouseover and mouseout). In other cases, pass null.
);
document.dispatchEvent(mouseMoveEvent)
@hsluoyz
Copy link

hsluoyz commented Nov 25, 2019

Should use new MouseEvent()

@diegohaz
Copy link

new MouseEvent() doesn't work on IE 11

@abrman
Copy link

abrman commented Apr 7, 2023

While using popup window portals in react, some of my components hooked onto window and didn't behave properly. Passing event list seen below worked well for me.

popup.addEventListener("mousemove", (e) => {
const evt = new MouseEvent("mousemove", {
  view: popup,
  bubbles: true,
  cancelable: true,
  clientX: e.clientX,
  clientY: e.clientY,
  screenX: e.screenX,
  screenY: e.screenY,
});
window.dispatchEvent(evt);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment