Search code examples
google-chromeeventsmousewheeldispatch

chrome dispatching wheel event


I'm trying to dispatch a wheel event in chrome but still can't make it. I am using the WheelEvent object but seems that just cannot "init" it right. Whatever I do, delta is always 0. I looked at the specification, but no help. More interesting, I captured the event when I actually scroll with mouse wheel and tried to dispatch that event, but again the deltas were 0. Did anyone encountered such problem? Is this maybe a bug? Any help would be great!

//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)
}

Solution

  • At first I didn't need anymore this functionality but I recently got back to this issue and found a way to dispatch wheel event properly on Chrome. Sorry for the delay, I totally forgot about this issue. The code is as follows:

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

    More information can be found here. Hope this helps.