2011-07-20 34 views
3

我试图在chrome中调度轮子事件,但仍然无法做到。我正在使用WheelEvent对象,但似乎只是不能“初始化”它的权利。无论我做什么,三角洲总是0.我看着specification,但没有帮助。更有意思的是,当我用鼠标滚轮实际滚动并试图发送该事件时,我捕获了该事件,但再次出现增量为0.有没有人遇到过这样的问题?这可能是一个错误?任何帮助将是伟大的!铬调度轮事件

//dispatching the wheel event 
var evt = document.createEvent("WheelEvent"); 
evt.initEvent("mousewheel", true, true, null, 0, 0, 0, 0, 0, false, false, false, false, 0, null, -120); 
window.dispatchEvent(evt) 

// catching the wheel event 
window.addEventListener('mousewheel', callback, true); 
callback = function(evt){ 
     console.log(evt) 
} 
+1

请给我们一些代码。你如何注册事件监听器,或者你只是设置回调?你怎么派发这个活动? – Peter

+0

我添加了代码。事件被捕获,但页面不滚动,因为增量为0. – bellpeace

+0

好吧,我已经建立了原因。我使用了错误的init函数。正确的是[this](http://developer.apple.com/library/safari/#documentation/appleapplications/reference/WebKitDOMRef/WheelEvent_idl/Classes/WheelEvent/index.html)。但是我的页面仍然拒绝向下滚动。 – bellpeace

回答

2

起初我不需要这个功能,但我最近回到了这个问题,并找到了一种在Chrome上正确调度wheel事件的方法。对不起,我完全忘记了这个问题。代码如下:

var evt = document.createEvent("WheelEvent"); 
evt.initWebKitWheelEvent(deltaX, deltaY, window, screenX, screenY, clientX, 
         clientY, ctrlKey, altKey, shiftKey, metaKey); 
node.dispatchEvent(evt); 

更多信息可查询here。 希望这有助于。

+0

由于已从Chrome中删除,因此无法使用:https://groups.google.com/a/chromium.org/forum/#!searchin/blink-dev/initWebkitWheelEvent$20/blink-dev/h_GoezQ4MQ4/ Q5abFkhA76sJ –