How to send mouse wheel event to older IE

I want to create and fire / send a mousewheel event in IE8, which I can track in an event handler.

In IE8 document.createEvent == undefined, so I tried this, but without success:

var evt = document.createEventObject();
view.fireEvent('onMouseWheelEvent');

I would like to trigger and listen to mouse wheel events in IE7 ~ 9.

Any pointer would be helpful, thanks.

+4
source share
3 answers

You need to pass the event object to the arguments of fireEvent, not just the type of event.

var evt = document.createEventObject();
view.fireEvent('onMouseWheelEvent', evt);

Check for document.createEvent if(document.createEvent)and do either above or

var evt = document.createEvent('MouseWheelEvent');
evt.initMouseWheelEvent( evt, true, true );
view.dispatchEvent( evt );

. http://msdn.microsoft.com/en-us/library/windows/apps/hh453179.aspx .

0

IE 8 FireFox:

var triggerButton = document.getElementById("triggerMouseWheel");

triggerButton.onclick = function() {
    if (document.createEvent) {

        var mouseEvent = document.createEvent('MouseEvent');
        mouseEvent.initMouseEvent(
            'DOMMouseScroll',
            true, true, window, 120, 0, 0, 0, 0, 0, 0, 0, 0, 0, null
        );
        document.dispatchEvent(mouseEvent);

    } else if (document.createEventObject) {

        var mousewheelEvent = document.createEventObject(window.event);
        document.fireEvent("onmousewheel", mousewheelEvent);

    }
}

, createEventObject, IE8 , .

, , :

function addMouseWheelEvent(element, mouseWheelHandler) {
    if (element.addEventListener) {
        // IE9, Chrome, Safari, Opera
        element.addEventListener("mousewheel", mouseWheelHandler, false);
        // Firefox
        element.addEventListener("DOMMouseScroll", mouseWheelHandler, false);
    }
    // IE 6/7/8
    else element.attachEvent("onmousewheel", mouseWheelHandler);
}

addMouseWheelEvent(document, function() { alert("triggered"); });
0

It looks like you might be out of luck as far as IE8 is supported. I find quite a lot of evidence ( Microsoft / Google , Mozilla , Facebook ) that IE8 handles mouse / scroll events in such a way that you cannot work around them.

-1
source

Source: https://habr.com/ru/post/1542345/


All Articles