How to set pageX for simulated javascript event

穿精又带淫゛_ 提交于 2019-12-02 21:26:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!