如何为模拟的javascript事件设置pageX

时间:2013-01-31 04:21:16

标签: javascript events javascript-events mousemove

我通过以下方式模拟DOM节点上的mousemove事件:

var e = document.createEvent('MouseEvents');
e.initEvent('mousemove', true, true)
node.dispatchEvent(e)

但是在文档中绑定的mousemove回调函数中,返回的事件对象的pageX为0.有没有办法在初始化或调度时设置pageX事件?

1 个答案:

答案 0 :(得分:0)

来自here

要看到它正常工作,请运行:

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);

似乎clientXclientYpageXpageY相同! 希望它有所帮助!