YUI Library Home

YUI Library Examples: ImageLoader Utility: Basic Features of the ImageLoader Utility

ImageLoader Utility: Basic Features of the ImageLoader Utility

Displayed here are the basic abilities of the ImageLoader Utility. Images are loaded only after triggers are fired or time limits are reached.

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

Basic Features of the ImageLoader Utility

The ImageLoader Utility allows you to define the conditions under which images are loaded into the page. This example demonstrates attaching some simple triggers to images to establish this load control.

The HTML used for the image needs little to no modification. Simply make sure the image elements have id attributes, eliminate the src attribute from <img> elements, and optionally declare <img> elements as having hidden visibility.

1<div class='everything' id='everything'
2    <div class='topmain' id='topmain'></div> 
3    <div class='duo1' id='duo1'></div> 
4    <div class='duo2' id='duo2'></div> 
5    <div class='png' id='pngimg'></div> 
6    <div class='scroll'
7        <img id='scrollImg' style='visibility:hidden;' /> 
8    </div> 
9</div> 
view plain | print | ?

In JavaScript, create one ImageLoader group for each set of images and register each image with the group. Let's step through the groups one by one.

Starting with the architectural image at the top. We set a mouseover of the image itself as a trigger, and a 2-second time limit.

1var mainGroup = new YAHOO.util.ImageLoader.group('topmain''mouseover', 2); 
2mainGroup.registerBgImage('topmain''http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/museum.jpg'); 
view plain | print | ?

The second group has two images, and also two triggers. As triggers, we have a mouseover of the left image and a click on the right image.

1var duoGroup = new YAHOO.util.ImageLoader.group('duo1''mouseover', 4); 
2duoGroup.registerBgImage('duo1''http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/uluru.jpg'); 
3duoGroup.registerBgImage('duo2''http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/katatjuta.jpg'); 
4duoGroup.addTrigger('duo2''click'); 
view plain | print | ?

The third group has no trigger; the only thing that will load the image is its time limit of 5 seconds.

1var pngGroup = new YAHOO.util.ImageLoader.group(nullnull, 5); 
2pngGroup.registerPngBgImage('pngimg''http://l.yimg.com/a/i/us/nws/weather/gr/47s.png'); 
view plain | print | ?

In the last group, we have an image loaded by the window's scroll event. Since this is an <img> element, we omit the src attribute. Also, we'll set the visibility to hidden to avoid a broken image icon. (Our other option is to use a transparent image as the source, but the tradeoff is the load burden of this additional image.) Because we've hidden the image, we need to make sure it gets changed to visible via the setVisible flag. This group has no time limit, so the only thing that will load the image is its scroll trigger.

1var scrollGroup = new YAHOO.util.ImageLoader.group(window, 'scroll'); 
2var scrollImg = scrollGroup.registerSrcImage('scrollImg''http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/morraine.jpg'); 
3scrollImg.setVisible = true
view plain | print | ?

Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

YUI Logger Output:

Logger Console

INFO 505ms (+177) 10:15:42 PM:

LogReader instance0

LogReader initialized

INFO 328ms (+0) 10:15:42 PM:

Get

Appending node: ../../../2.x/build/event-mouseenter/event-mouseenter-min.js

INFO 328ms (+0) 10:15:42 PM:

Get

attempting to load ../../../2.x/build/event-mouseenter/event-mouseenter-min.js

INFO 328ms (+328) 10:15:42 PM:

Get

_next: q0, loaded: undefined

INFO 0ms (+0) 10:15:42 PM:

global

Logger initialized

More ImageLoader Utility Resources:

Copyright © 2011 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings