Scroll browser window inside Flash

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:

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

and in your movie:
[as] var mouseListener:Object = new Object();
mouseListener.onMouseWheel = function(delta:Number) {
getURL(“javascript:window.scroll(getScrollXY()[0], getScrollXY()[1] + “+(0-delta*8)+”);”);
};
Mouse.addListener(mouseListener);
[/as] 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:
[as] mouseListener.onMouseWheel = function(delta:Number) {
if(delta > 0) {
getURL(“javascript:window.scroll(getScrollXY()[0], “+this.bottom+”);”);
}
else {
getURL(“javascript:window.scroll(getScrollXY()[0], “+this.top+”);”);
}
};
[/as]

5 thoughts on “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:

    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.

  3. 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.

Comments are closed.