Search code examples
javascriptfirefoxflashscrolldom-events

Prevent mouse wheel events from being processed by Flash in Firefox


I'm currently working on an app that has some legacy Flash pages. We recently added a dropdown in the header that opens into a scrollable div. The header and dropdown are HTML.

Here's a wireframe to illustrate the set-up.

https://wireframe.cc/27ahkk

We ran into an issue where using the scroll wheel while hovering in the dropdown did not behave as desired.

  • Dropdown does not scroll
  • Flash content scrolls instead

I expected this was because, as part of Flash's processing of the wheel/mousewheel events, it was preventing bubbling and preventing the default action. I did some initial tests in Chrome and found that any event listeners I attached to the bubble phase were in fact not called.

My solution was to attach a wheel/mousewheel event listener to the window on the capture phase. In that handler, if the mouse is hovering over our dropdown, I call event.stopPropagation() to prevent the event from ever reaching Flash (thus allowing the default action to occur). Roughly:

window.addEventListener("wheel", function (event) { 
    if ($('.dropdown-selector:hover').length != 0) {
        event.stopPropagation();
    }
}, true);

This event is removed when the dropdown closes.

This worked fine in Chrome, and it works in IE as long as you fall back to the deprecated "mousewheel" event instead of using the more modern "wheel" event. Firefox, however, is a different story.

While Firefox supports the "wheel" event, it seems that the way in which events are sent to Flash from the browser is entirely different.

If you are hovering over the dropdown and scroll with the mouse wheel:

  1. The handler will fire
  2. The if condition properly detects the hover
  3. event.stopPropagation() is called
  4. However, Flash content still scrolls
  5. Dropdown div does not scroll

Even stranger, if you are not hovering over the dropdown and you scroll with the mouse wheel:

  1. The handler never fires
  2. Flash content scrolls

This is different than the observed behavior in Chrome and IE, where scrolling while hovering over Flash will still fire the handler, but since the mouse is not over the dropdown, event.stopPropagation() is never called and thus it captures down to Flash where the event will be handled.

This is confusing to me because I attached the listener to the window in capture phase, so I should be receiving the event before anything else on the page. But in Firefox, it appears that either Flash gets the events even before window, or Flash receives a different event altogether (Flash appears to receive these events even if the JavaScript is stopped on a breakpoint in the debugger).

Does anyone have experience with Flash and Firefox and have a better understanding of how the browser sends events to embedded Flash content? Why does my strategy work in all the other browsers but fall short in Firefox? Any possible workarounds before we try to delve into the Flash code itself to work on a solution?


Solution

  • So I found a fix for this issue, although not entirely sure why it works. Even though Firefox supports both the "wheel" and "mousewheel" events, registering our listener to these events did not produce desired behavior. However, listening to the "DOMMouseScroll" event worked beautifully.

    Not sure if this is due to some browser-specific logic and/or how our legacy Flash code handles scrolling, but it works so I'm satisifed. :)