Tweening to a Relative Position using a Variable in TweenMax

This isn’t too complicated, but it’s something I hadn’t come across until a few days ago. If you want to tween an object relatively, but the new value changes and needs to be a variable, just cast it as a string.

// Your everyday absolute tween that moves movieclip_mc
// to 100 on the x axis, taking one second to do so
TweenMax.to(movieclip_mc, 1, {x:100});

// A relative tween that moves movieclip_mc to a
// position on the x axis 100 units/pixels greater
// than movieclip_mc's current position
TweenMax.to(movieclip_mc, 1, {x:"100"});

// An absolute tween like the first, but passing in
// the new value with a variable
TweenMax.to(movieclip_mc, 1, {x:newX});

// A tween that casts the variable as a string,
// thus making it a relative tween
TweenMax.to(movieclip_mc, 1, {x:String(newX)});

If you’re not using TweenMax/TweenLite, do yourself a favor and give it a try.

Easily Test & View Alternative Content while Using SWFObject

SWFObject is great. I’ve been using it to embed Flash content in my pages since the IE / Eolas “click to activate” thing when it was still called FlashObject.

But SWFObject is missing a feature. It’s not easy enough to test your non-Flash alternative content. You can bump up the required version number to one that doesn’t exist yet, which is a good way to make sure your expressInstall.swf is working. Or you can follow the directions in their FAQ and disable JavaScript or the Flash Player (17. How to disable Flash Player or JavaScript, e.g. to test alternative content?).

Those solutions take a bit more effort than should be required. What if you want to show a client at another location the alternative content? I’d rather send them a link than walk them through disabling/enabling their JavaScript. And what if you want to take it beyond just testing, and give your users in a live environment the option to view the non-Flash content?

Here’s what I do. The SWFObject API includes a useful method to get query string parameters, ingeniously called getQueryParamValue(), which can be used to grab query string information and then pass it into the SWF via the flashvars array. In this example, I use the method to determine whether or not to embed the SWF in the first place.

Your standard SWFObject embed code will look something like this:

<script type="text/javascript">
	var flashvars = {};
	var params = {};
	var attributes = {};
	attributes.id = "flashContent";
	swfobject.embedSWF("flashContent.swf", "alternativeContent", "100%", "100%", "9.0.28", "expressInstall.swf", flashvars, params, attributes);
</script>

Here’s my version with a simple if statement to determine if the non-Flash content should be shown.

<script type="text/javascript">
	var flashvars = {};
	var params = {};
	var attributes = {};
	attributes.id = "flashContent";
	if (swfobject.getQueryParamValue("viewAlt") == "true") {
		// This lets us test the alternative content. If "viewAlt" is true in the query string, we don't allow SWFObject to replace the alternativeContent div with the Flash content.
	}else{
		swfobject.embedSWF("flashContent.swf", "alternativeContent", "100%", "100%", "9.0.28", "expressInstall.swf", flashvars, params, attributes);
	}
</script>

So all it takes to view and test the alternative content is to throw ?viewAlt=true at the end of your URL. This will stop SWFObject from ever trying to embed your SWF.

Here is an example of this process in action.

And here is the source.

Forcing Vertical Scrollbar Gutter to Show to Prevent Page Shift while Using swffit

Update: This fix is no longer necessary. When explaining it to a coworker, we discovered a new version of swffit with a new method called showScrollV() that does the same thing.

A common problem with centered web site layouts is what’s been dubbed “page shift.” This happens as users move among pages that are too short for a vertical scrollbar and those that are long enough to require them, or when a page has expanding and collapsing content which forces the scrollbar to appear and disappear as the content is manipulated. When the scrollbar’s state changes, the page recenters itself so the content jumps back and forth horizontally.

Page shift is especially distracting on a page where the content resizes without refreshing the page (like in Flash) because you can see everything move. When you’re clicking through different pages, it’s not as big a deal because a new page wipes out items for a split second before the shift occurs. Nonetheless, it can be distracting and there is a CSS fix for it.

html {
overflow-y: scroll;
}

This tells the browser that we’ll be scrolling things vertically, so keep the y scrollbar ready (IE always keeps the scrollbar gutter there, but it’s an issue on other browsers).

When it comes to Flash sites, using the browser’s built in scrollbars is a great technique. Some people like to create their own scrollbars in Flash, but that takes time and things get a bit more complicated when you want to get the scroll wheel working. Why not use the built in version, whose usability is already established, instead of reinventing the (scroll – heh) wheel?

Luckily we have a great script, swffit, that can help us use our browsers’ scrollbars by allowing us to change our SWF’s height on demand. Unfortunately, swffit overwrites the CSS that fixes the page shift problem, so I had to adjust the script a touch. In swffit.js, search for

html.style.overflow="auto";

and change it to

html.style.overflowX="auto";
html.style.overflowY="scroll";

and you’ll be able to use swffit without seeing any page shift.