I recently stumbled over a new AS3 gotcha that seems to be bugging a lot of the Flash community. Apparently a new 'feature' of AS3, whereby using the mousewheel inside an embedded Flash movie still scrolls the surrounding browser window (if it has enough content, that is).
This didn't happen in AS2. If a Flash movie had focus, mousewheel events weren't sent to the browser window. It wasn't a problem in FireFox either, until a recent update and adversely affects any AS3 Flash app embedded in an HTML page where you might want to scroll or zoom with the mousewheel while over the Flash, instead of the scrolling the browser window.
So, I decided to cook up some code to fix this. While it's early version and isn't perfect, it suits my purposes, is easy to set up and doesn't require any external JavaScript - so I'd thought I'd share it with the world, in case Adobe never get around to addressing this 'feature'.
Introducing MouseWheelTrap. A handy little utility class that traps mousewheel events while the mouse is over the Flash, so your app scrolls how it was intended to. I've tested it with SWFObject on PC IE and FireFox, but not with SWFMacMouseWheel on Mac (if someone could tell me if it works). I decided against using jQuery for trapping mouse events, partly out of laziness, partly because many people have no control over the HTML that embeds their Flash app and therefore can't add custom JavaScript.
Setting up MouseWheelTrap is easy:
-
import com.spikything.utils.MouseWheelTrap;
-
MouseWheelTrap.setup(stage);
Simple huh? Let me know how you get on...
Pingback: Tweets that mention MouseWheelTrap – how to stop simultaneous Flash / browser scrolling in AS3 at Liam O'Donnell -- Topsy.com
Wonderful, worked great for me. I was having the issue in IE (FireFox was working fine for me) -- adding this fix immediately solved the problem.
This worked great Liam.
thanks
you don't know how hard I was looking on the internet for a solution.
I thought the answer would be in JavaScript...but you made it easier.
thanks
Actually, it does use JavaScript, but the code is injected at runtime by the Flash - which means you still needs scripting access allowed, but at least you don't need an external JS file too
Note that I've now hosted the code at http://code.google.com/p/mousewheeltrap/
It works great in firefox and safari. In IE and chrome I must first click on my map (.swf). Once I do this it works great fine. Could this be a javascript issue? I'm using flex 3 so the only changes I've made were to the addEventListeners.
FROM
mx.core.FlexGlobals.topLevelApplication.addEventListener(MouseEvent.ROLL_OUT, function():void
TO
Application.application.addEventListener(MouseEvent.ROLL_OUT, function():void
sample page (the map at bottom of page) : http://crownpeak.vipnet.org/cmsportal3/
Thanks in advance,
Mark
Melmendo, I'd say that could be to do with the fact that Flex builds its children in the background and isn't actually quite ready when you expect it. If you have a search MouseWheelTrap, I think a few people out there adapted it for better Flex support - I will look into it.
Hey there,
it's really nice solution you posted. Helped me out alot!
However, while testing my flash app (not very optimized and many animations and data processing going on) on various browsers. I've noticed that the app lags A LOT in Opera. I first tried to replace eval with xml-formated js, then moved the js eval section of your code to the html header, still no good. Then analyzed the code a little bit closer and changed
stage.addEventListener(MouseEvent.MOUSE_MOVE, function():void {
to
stage.addEventListener(MouseEvent.MOUSE_OVER, function():void {
It's completely sufficient to use MOUSE_OVER event type to initiate MouseWheelTrap, since otherwise the flash has to send request to the javascript every single time the cursor moves around. This little correction brings a slight performance boost in IE/FF/Chrome and a HUGE ONE in Opera (v 10.50).
Hope this helps anyone.
Yeah, good point. I think there was I reason I used MOUSE_MOVE instead (probably to do with right-clicking or something). It may be a left over from a stage.frameRate idling thing I did that worked nicely (since this had internal logic to ensure it only updates when necessary).
When I get some time, I'll test the change and implement it if I can't break it
I'll also format the JavaScript as XML for clarity - thanks for the heads up!