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:
JAVASCRIPT:
-
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:
Actionscript:
-
var mouseListener:Object = new Object();
-
mouseListener.onMouseWheel = function(delta:Number) {
-
getURL("javascript:window.scroll(getScrollXY()[0], getScrollXY()[1] + "+(0-delta*8)+");");
-
};
-
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:
-
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+");");
-
}
-
};
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
Comment by tzav — December 26, 2006 @ 11:37 amThanks
Comment by Madarco — December 27, 2006 @ 10:00 pmGood but ExternalInterface better that getURL. =P
Comment by AndrĂ© Ponce — February 23, 2007 @ 6:15 pmYes 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.
Comment by Madarco — February 24, 2007 @ 12:16 amHi, 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.
Comment by LadySamG — November 1, 2007 @ 5:27 pm