Scroll browser window inside Flash

Posted in Flash at 2:13 pm by Madarco

Updated: fixed some typos

I've found that resizing a flash movie to fit his content has a little drawback: the mouse wheel doesn't scroll the browser window when the movie has focus, and in my case the movie has width: 100%.
Luckily I've found a nice javascript to scroll the page.

Add this javascript to the page:

JAVASCRIPT:
  1. function getScrollXY() {
  2.   var scrOfX = 0, scrOfY = 0;
  3.   if( typeof( window.pageYOffset ) == 'number' ) {
  4.     //Netscape compliant
  5.     scrOfY = window.pageYOffset;
  6.     scrOfX = window.pageXOffset;
  7.   } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
  8.     //DOM compliant
  9.     scrOfY = document.body.scrollTop;
  10.     scrOfX = document.body.scrollLeft;
  11.   } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
  12.     //IE6 standards compliant mode
  13.     scrOfY = document.documentElement.scrollTop;
  14.     scrOfX = document.documentElement.scrollLeft;
  15.   }
  16.   return [ scrOfX, scrOfY ];
  17. }

and in your movie:

Actionscript:
  1. var mouseListener:Object = new Object();
  2. mouseListener.onMouseWheel = function(delta:Number) {
  3.   getURL("javascript:window.scroll(getScrollXY()[0], getScrollXY()[1] + "+(0-delta*8)+");");
  4. };
  5. Mouse.addListener(mouseListener);

I multiply to 8 the delta to adjust the mouse sensitivity. Sadly there isn't a way to find the user's system sensitivity.
Maybe a better way can be to scroll directly to a specific point in the page:

Actionscript:
  1. mouseListener.onMouseWheel = function(delta:Number) {
  2.   if(delta> 0) {
  3.     getURL("javascript:window.scroll(getScrollXY()[0], "+this.bottom+");");
  4.   }
  5.   else {
  6.     getURL("javascript:window.scroll(getScrollXY()[0], "+this.top+");");
  7.   }
  8. };

5 Responses to “Scroll browser window inside Flash”

  1. tzav Says:

    Thanks a lot for your script, it helped a great deal.
    However there is a small syntax problem on the code your wrote :
    On both actionscript script you wrote you forgot the ' ); '
    That is at the end of the getURL lines...
    Probably one of those ugly copy and paste almost all of it type of problem.

    Thanks again

    Fred

  2. Madarco Says:

    Thanks :)

  3. André Ponce Says:

    Good but ExternalInterface better that getURL. =P

  4. Madarco Says:

    Yes but ExternalInterface is for Flash >= 8 and IMHO its useful only if you want to make a synchronous call, in this case I don't think it's required.

  5. LadySamG Says:

    Hi, I tried the other guys code and it worked, but I can't seem to get yours to work for me. I put your plain text in my webpage and the actionscript the frame of a layer in my flash file. I tried debugging and the listener works in the .swf itself but not when embedded in the html. I have my swf object in a should I do this?

    I'm a newbie to flash btw.

Leave a Reply

Antispam: