问题
I'm simulating a mousemove
event on a DOM node by:
var e = document.createEvent('MouseEvents');
e.initEvent('mousemove', true, true)
node.dispatchEvent(e)
However in my mousemove
callback which is bound on the document, the event object is returned with a pageX
of 0. Is there a way I can set pageX
when initializing or dispatching the event?
回答1:
From here:
To see it working, just run this:
document.addEventListener('mousemove', function (e) { console.log(e.pageX); });
var e = document.createEvent('MouseEvents');
// event.initMouseEvent(type, canBubble, cancelable, view, detail, screenX, screenY, clientX, clientY)...
e.initMouseEvent('mousemove', true, true, window, 1, 100, 100, 100, 100);
document.dispatchEvent(e);
Seems that clientX
and clientY
are the same as pageX
and pageY
!
Hope it helps!
来源:https://stackoverflow.com/questions/14618426/how-to-set-pagex-for-simulated-javascript-event