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
Yahoo! Library
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
Re: Yahoo! Library
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.
Dude
I suppose I could do a quickie 'if safari don't run', but I'd rather have it work :)
Thanks!
Re: Dude
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.
No title
Of course it could be something else in my page - I'm no expert here :)
Re: No title
Strange. I'll try to rustle up a Mac and see if I can figure out what the problem is.
No title
Errr.. Must be something in your pants... lol
Re: No title
Interesting. I can't think of any reason why a dd would work and a div wouldn't. That's very odd.
web developer
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.