The eclectic musings of a bitter software engineer.

In part 1 I showed you how to speed up page rendering by moving a remote JavaScript include (a Flickr badge in the example) into a hidden <div> at the bottom of the page and using a few lines of code to drop it into the desired location once it it's loaded. In this article, I'll show you how to use the Yahoo! UI Library's Animation Utility to make your dynamically-loaded content fade in smoothly so your visitors aren't distracted by its sudden appearance.

First we need to load the YUI libraries. To do this, we'll add the following lines just above our hidden "fakeflickr" <div> from part 1:

<script src="http://yui.yahooapis.com/2.2.2/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script src="http://yui.yahooapis.com/2.2.2/build/animation/animation-min.js"></script>

<div id="fakeflickr" style="display: none;">
  <script src="http://www.flickr.com/[...]"></script>
</div>

This will load the necessary libraries from Yahoo!'s own servers, which are so fast I heard they once did the Kessel Run in less than twelve parsecs.

Next, we'll need to add a teeny bit of CSS to give our "flickr" <dd> element a fixed height. This will keep the page layout from having to adjust when we populate that element with the Flickr badge content.

To figure out what height you should use, I recommend using Firebug to inspect the element and see what its height is once the badge is loaded. Then use that number in the CSS below, which you can either put inside a <style> tag in the page header or add to an external CSS file:

#flickr {
  background: url('loading.gif') no-repeat 48% 48%;
  height: 161px;
}

Notice the background image? You can grab that from ajaxload.info. It'll clue our visitors into the fact that the content in that area is being loaded in the background, so they'll be less surprised when they see it fade in.

Now all we need to do is make a few modifications to our JavaScript snippet from part 1 to make the Flickr badge fade in:

<script type="text/javascript">
  var flickr     = document.getElementById('flickr'),
      fakeflickr = document.getElementById('fakeflickr'),
      anim       = new YAHOO.util.Anim(flickr, {opacity: {to: 1.0}}, 1,
          YAHOO.util.Easing.easeBoth);

  YAHOO.util.Dom.setStyle(flickr, 'opacity', 0.0);
  YAHOO.util.Dom.setStyle(flickr, 'background', 'none');

  flickr.innerHTML = fakeflickr.innerHTML;
  fakeflickr.innerHTML = '';

  anim.animate();
</script>

And we're done. Easier than you thought it'd be, wasn't it?

Comments

I noticed you used the Yahoo! UI Library in the example. Is this because you consider it to be the best/easiest/whatnot, or does it have something to do with your employment at Yahoo!? And if it is because of your employment, which library would you, as a person, recommend? Inquiring minds want to know

Sunday April 22, 2007 @ 12:04 PM (PDT) Posted by Anonymous Coward

Believe it or not, I was actually using the YUI libraries long before I was hired by Yahoo!. I've been using them for over a year now, and in general I think they do a better job than most other libraries at providing important functionality while remaining lightweight and easy to use. They're also very well documented, which isn't true of many JavaScript libraries.

They do have their shortcomings, though. If you want to do something really fancy, YUI can help, but it's not going to do all the work for you like some other libraries. That's where I'd recommend taking a look at Jack Slocum's Ext JS (formerly called YUI-ext) or others.

Sunday April 22, 2007 @ 01:47 PM (PDT) Posted by Ryan Grove
Awesome code. It's really helped a lot! But - ;) Safari doesn't seem to like the animation code. So, what I really wanted - faster load time - worked great, the nifty animation isn't 'universal' enough. Do you know of any work arounds?

I suppose I could do a quickie 'if safari don't run', but I'd rather have it work :)

Thanks!
Monday May 07, 2007 @ 03:38 PM (PDT) Posted by Havokmon

The YUI animation library should work fine in Safari 2.0 on OS X 10.4. Any other version isn't guaranteed to work.

Unfortunately I don't have a Mac to test with, but if you're using Safari 2.0 and OS X 10.4 and still can't get it working, let me know and I'll send an email to the YUI team.

Monday May 07, 2007 @ 05:02 PM (PDT) Posted by Ryan Grove
I'm running Safari 2.0.3 on OSX 10.4.6. My 'live' page is www.vfemail.net. It almost seems like it executes out of order. The animation runs, but then after it's done, it seems the opacity goes back to 0.

Of course it could be something else in my page - I'm no expert here :)
Monday May 07, 2007 @ 05:28 PM (PDT) Posted by Havokmon

Strange. I'll try to rustle up a Mac and see if I can figure out what the problem is.

Monday May 07, 2007 @ 07:03 PM (PDT) Posted by Ryan Grove
Update - the animate function works fine, but the demo code adapted (I used div instead of dd *shrug*) doesn't work in Safari. Your page (duh - should have checked that) does work just fine.

Errr.. Must be something in your pants... lol
Monday May 07, 2007 @ 07:04 PM (PDT) Posted by Havokmon

Interesting. I can't think of any reason why a dd would work and a div wouldn't. That's very odd.

Monday May 07, 2007 @ 08:51 PM (PDT) Posted by Ryan Grove

I've had multiple problems with safari setting opacity back to 0 after animating to 1. I always took it as a silly bug and solved it with a branch. I set the opacity attribute for safari browsers to .99 instead of to 1. I guess I should check to see if it's been reported one of these days.

Thursday May 24, 2007 @ 11:06 PM (PDT) Posted by druniontest
Post a comment

Basic XHTML (including links) is allowed, just don't try anything fishy. Your comment will be auto-formatted unless you use your own <p> tags for formatting. You're also welcome to use Textile.

Don't type anything here unless you're an evil robot:


And especially don't type anything here:

Copyright © 2002-2008 Ryan Grove. All rights reserved.
Powered by Thoth.