Fullscreen flash with browser scrollbar

29 October 2008

A lot of people have blogged about this and there have been a few attempts at libraries which solves the problem. Some have gained status and some have gracefully failed silently. It’s only recently that I encountered the need to use this method and frankly I prefer browser scrollbars as most users do in my opinion. I feel they are more responsive and have better performance. But damn it ain’t easy getting this to work properly and at one point I wondered if it is actually worth the trouble.

Quick overview of the problem

It may be worth pointing out that in most cases there are two distinct reasons a flash website might have, for wanting to enable the browser scrollbar.

  • If the window is resized to a smaller size that was designed for - a scrollbar would be nice.
  • If the content is larger than the original design - a scrollbar would be nice

SWFResize, SWFForcesize and SWFObject

SWFResize is a nice little add-on to the SWFObject solution. Inducted into the OSFlash hall of fame, it is with regret that this add-on doesn’t work anymore. Reasons not being for lack of development but rather the release of SWFObject version 2. Enter SWFForcesize, another attempt to solve the fullscreen flash with browser scrollbar. Very similar to SWFResize this add-on too worked quite well but also only with SWFObject version prior to 2.

We all love SWFObject. It’s the next best thing since sliced bread. It kicked dust in all the proverbial cowboy faces and irritates the crap out of Jakob Nielson purists. Before the release of the mighty version 2, SWFObject seemed to work quite well with the afore mentioned add-ons.

Lucky for us Bobby van der Sluis and Geoff Stearns did some sterling work and included an example for solving the first requirement when showing full flash with a browser scrollbar in the latest version of SWFObject. This doesn’t however solve the problem of content being larger than the original design. For this we need to make some changes to Bobby’s example code.

SWFObject original code

<script type="text/javascript">
//<![CDATA[
function getViewportSize() { 
    var size = [0, 0]; 
    if (typeof window.innerWidth != "undefined") { 
        size = [window.innerWidth, window.innerHeight];
    } 
    else if (typeof document.documentElement != "undefined" && 
         typeof document.documentElement.clientWidth != "undefined" && 
         document.documentElement.clientWidth != 0) {
        size = [document.documentElement.clientWidth, document.documentElement.clientHeight]; 
    }
    else {
        size = [document.getElementsByTagName("body")[0].clientWidth, 
            document.getElementsByTagName("body")[0].clientHeight]; 
    }
    return size; 
}
function createFullBrowserFlash() {
    swfobject.createCSS("html", "height:100%;");
    swfobject.createCSS("body", "height:100%;");
    swfobject.createCSS("#container", "margin:0; width:100%; height:100%; min-width:980px; min-height:553px;");
    window.onresize = function() {
        var el = document.getElementById("container");
        var size = getViewportSize(); 
        el.style.width = size[0] < 980 ? "980px" : "100%";
        el.style.height = size[1] < 553 ? "553px" : "100%";
    };
    window.onresize();
}   
swfobject.embedSWF("main.swf", "content", "100%", "100%", "9.0.0");
if (swfobject.hasFlashPlayerVersion("9.0.0")) {
    swfobject.addDomLoadEvent(createFullBrowserFlash);
}
//]]>
</script>

The solution

<script type="text/javascript">
//<![CDATA[

var size = getViewportSize();    
function getViewportSize() 
{ 
    var size = [0, 0]; 
    if (typeof window.innerWidth != "undefined") { 
        size = [window.innerWidth, window.innerHeight];
    } 
    else if (typeof document.documentElement != "undefined" && 
         typeof document.documentElement.clientWidth != "undefined" && 
         document.documentElement.clientWidth != 0) {
        size = [document.documentElement.clientWidth, document.documentElement.clientHeight]; 
    }
    else {
        size = [document.getElementsByTagName("body")[0].clientWidth, 
            document.getElementsByTagName("body")[0].clientHeight]; 
    }
    return size; 
}
function createFullBrowserFlash() 
{
    swfobject.createCSS("html", "height:100%;");
    swfobject.createCSS("body", "height:100%;");
    swfobject.createCSS("#container", "margin:0; width:100%; height:100%; min-width:980px; min-height:553px;");
}   
function setHeight( h )
{
    if ( h < size[1] ) h = "100%";
    var el = document.getElementById("container");
    if ( h != "100%" ) el.style.height = h < 553 ? "553px" : h + "px";
    else el.style.height = "100%";
}

var flashvars = { };
var params = { "allowscriptaccess": "samedomain" };

swfobject.embedSWF("main.swf", "content", "100%", "100%", "9.0.0", false, flashvars, params);
if (swfobject.hasFlashPlayerVersion("9.0.0")) {
    swfobject.addDomLoadEvent( createFullBrowserFlash );
}
//]]>
</script>

The basic changes are the removal of the window.resize listener as this is now handled through Actionscript and the addition of a setHeight() Javascript method. This method will be called from each Flash page with a new height. It checks to see if the value supplied is less than the minimum height, if it is then use 100% as the height. If not, set the container div’s height to the new value. Easy peasy.

I only set the hight as that was all I required from the resize, although in some cases you might need to add the width too.

Selah.

Tested

IE6, IE7 FF 3.0.3 Safari 3.1

Resources

24 April 2011 | Kristian Lewis Jones

Where did you get the values for min height and min width from? some browsers have a bigger resolution to others... min height should be 100% ? I'm confused.

03 February 2010 | wezside

It's fixed. Refresh the page using Ctrl+R or clear your cache.

03 February 2010 | Shawn

I would love to see the "original code" as compared to the "solution" however when I load this page (In multiple browsers) the code that I would imagine to be there is not. it is just empty so I actually have no idea what the solution is and I would love to know as this is something that I am needing to do for a project right now. if you would not mind posting/ emailing me or whatever that would be great. thanks

05 May 2009 | zakinjo

This solution is just what I needed, but I have one problem and I am hoping you can help me. I dont know how to change the value of the js function setHeight that shows the srcoll bar if the content is larger than the original design.

Thanks in advance

03 March 2009 | Daniel Chapman

Greetings,

I have used your solution and it works great in IE7, FF and safari, however IE6 is struggling with the 100% height. Its not setting the flash heights properly.

Take a look at www.myxmeetings.com/welcome/ and navigate to the team page. Do you have any ideas or suggestions to fix this? If so please email me.

Thanks a bunch.

Daniel

01 March 2009 | Danny

Thanks a bunch this was immensely valuable to me! I can't thank you enough, cheers!

one note: I faced an issue in safari, where if anyone resized their browser after initial load, safari wasn't setting the flash content to 100% rather to the value it set var size at during page load.

So I kept getting HTML space near the bottom if i put my browser fullscreen, and the script set the 100% height.

I worked around this problem by including a size = getViewportSize; call within the setSize(h) function. Now it updates the size value and sets the flash content to 100% of the current browser viewport size, and not the size of intial load.

Hope thats useful :)

08 December 2008 | Edwin

This is very useful!

Thanks for this!

03 December 2008 | Andrew Cary

Hi

This seems like a great solution and is exactly what I am looking for to add to my new site. I'm not a coder and can at best adapt existing solutions to my design focused site.

I'm currently using this solution to enable the back button. http://exanimo.com/actionscript/statemanager/

I would love to be able to add this scrollbar functionality as well without breaking the statemanager stuff... is there an easy way to amend the code so that both can work? I would be deeply grateful if you could assist me.

Best regards, Andy

06 November 2008 | wezside

You are absolutely right. However, this doesn't solve the scenario of having larger content.

05 November 2008 | Geoff Watsson

Last night I discovered THE easiest way to Fullscreen flash. All you do, is you publish the flash file from flash with these settings: http://www.geoffwatsondesign.com/fullscreen/image1.jpg Then open the HTML file that Flash has created. You will see there's a base of script entitled "ACFLRunContent" and it begin's like this:

if (AC_FL_RunContent == 0) {
    alert("This page requires AC_RunActiveContent.js.");
} else {
    AC_FL_RunContent( 

then you'll notice a large amount of settings. Change the following:

Width: 100%
Height: height of desired amount you want to be scrollable

Then when you test your HTML page, you'll see that there are no horizontal scroll bars, however your vertical scrollbar beocomes enabled when you resize the window smaller then your designated height.

It's that easy!

30 October 2008 | Jeff

Thats two for SWFFit

30 October 2008 | George K

I'm using SWFFit and it's working perfectly with SWFObject 2.1

Incorrect please try again