YUI recommends YUI3.

YUI 2 has been deprecated since 2011. This site acts as an archive for files and documentation.

This documentation is no longer maintained.

YUI Library Examples: ImageLoader Utility: Using ImageLoader with CSS Class Names

ImageLoader Utility: Using ImageLoader with CSS Class Names

The ImageLoader Utility allows you an alternate method of using CSS class names to load images.

Hover over each image to show its triggers and limit. Try tripping the triggers to see the load reactions. Refresh the page to reset the images.

Using CSS Class Names to Load Images

Look familiar? These are the same images and triggers as the Basic Features example (with the weather image omitted). The only difference is the manner in which we're loading the images on the back end.

Insead of registering specific image ids/URLs with a group, you can simply tag the group with a CSS class. The group will later use this class name to identify which DOM elements belong to the group. Each group should have one corresponding class. Each class must have a background:none CSS definition at the top of the page, as in this example:

Here is the HTML for the images:

A few things to note. First, the images have class names matching those in the style definitions above.

Second, the image URL is set in the background-image of the elements. The background:none defined earlier in the CSS will be removed by the ImageLoader Utility JavaScript when the images are eventually loaded.

Third, since the <img> element displays its images through the background-image, its size won't change when the image is loaded. Therefore the width/height needs to be set in the HTML. And since that gives the image a substantial size, the browsers would show a missing-image icon if the src attribute were not specified. Therefore we need to set one; a transparent one so that the background image will show through.

This brings up an important limitation with this approach: you cannot alter the natural size of the image. Because the image is displayed as a background image, the browser will not resize the image according to the width/height of the <img> element.

Now let's turn to the JavaScript. Since the image URLs are already specified in the HTML, we don't need them in the JS. All each group needs to know is the CSS class name that will identify the images.

Note that you are free to combine this class-name approach with the other. The same group can have some images identified by class name and others by registering ids/URLs.

Copyright © 2013 Yahoo! Inc. All rights reserved.

Privacy Policy - Copyright Policy - Job Openings