5
<title>ImageLoader</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
17
<a href="#toc" class="jump">Jump to Table of Contents</a>
21
<div id="main" class="yui3-u">
22
<div class="content"><div class="intro">
24
The ImageLoader Utility allows you as an implementer to delay the loading of images on your web page until
25
such a time as your user is likely to see them. This can improve your overall pageload performance by deferring
26
the loading of some images that are not immediately visible at the time the page first renders.
27
Because images are often the heaviest components of a given page, deferring the loading of some images can
28
yield a marked improvement in the way the page "feels" to your user.
32
The ImageLoader Utility lets you determine triggers and time limits to initiate image loading.
33
This allows you to ensure that the images are loaded before your user is likely to see them.
34
This technique, obviously, is appropriate only for images that are not immediately visible to your user at initial page load.
38
<h2 id="getting-started">Getting Started</h2>
41
To include the source files for ImageLoader and its dependencies, first load
42
the YUI seed file if you haven't already loaded it.
45
<pre class="code prettyprint"><script src="http://yui.yahooapis.com/3.4.1/build/yui/yui-min.js"></script></pre>
49
Next, create a new YUI instance for your application and populate it with the
50
modules you need by specifying them as arguments to the <code>YUI().use()</code> method.
51
YUI will automatically load any dependencies required by the modules you
55
<pre class="code prettyprint">// Create a new YUI instance and populate it with the required modules.
56
YUI().use('imageloader', function (Y) {
57
// ImageLoader is available and ready for use. Add implementation
58
// code here.
63
For more information on creating YUI instances and on the
64
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_use"><code>use()</code> method</a>, see the
65
documentation for the <a href="../yui/index.html">YUI Global object</a>.
69
<h3 id="creating-an-image-group">Creating an Image Group</h3>
71
Images are organized into groups. Each group has one or more triggers. A trigger is simply any DOM event,
72
such as the <code>mouseover</code> of a specific <code><div></code>, a button click, or a window scroll.
73
The images in a group won't load into the page until this trigger fires. Groups also have an optional time limit;
74
if none of the group's triggers are activated before the time limit expires, the images are fetched anyway.
77
A group is defined as an instance of <code>Y.ImgLoadGroup</code>. It comprises a collection of images that will
78
show up on the page based on the same triggers and time limit.
80
<pre class="code prettyprint">var myFirstGroup = new Y.ImgLoadGroup({ timeLimit: 2 });
81
myFirstGroup.addTrigger('#someDivId', 'mouseover');</pre>
85
This defines a group triggered by a <code>mouseover</code> of the <code><div></code> with the HTML ID
86
of <code>someDivId</code>; with the <code>timeLimit</code> attribute we are specifying that all images
87
in <code>myFirstGroup</code> should load two seconds after the window's <code>load</code> event fires even
88
if our trigger hasn't been tripped.
91
<h3 id="adding-images-to-the-group">Adding Images to the Group</h3>
93
Register one or more images with the group using the HTML ID of the image element and the URL for the image:
95
<pre class="code prettyprint">myFirstGroup.registerImage({
96
domId: 'idOfDivWaitingForImage',
97
bgUrl: 'http://www.example.com/image/url'
102
This will set the image at <code>http://www.example.com/image/url</code> as the background image of
103
the <code><div></code> with the ID <code>idOfDivWaitingForImage</code>. There are three kinds of images you
104
can register with an ImageLoader group instance; see <a href="#addimages">Adding Images</a> below for more details on this process.
108
<h2 id="using">Using the ImageLoader Utility</h2>
110
This section describes how to use the ImageLoader Utility in further detail. It contains these subsections:
113
<li><a href="#performance">Performance Considerations</a></li>
114
<li><a href="#approach">Approach</a></li>
115
<li><a href="#triggers">Triggers</a></li>
116
<li><a href="#customtriggers">Custom Event Triggers</a></li>
117
<li><a href="#addimages">Adding Images</a></li>
118
<li><a href="#fold">Loading Images Below the Fold</a></li>
119
<li><a href="#visibility">Image Visibility</a></li>
120
<li><a href="#classnames">Using Class Names</a></li>
124
<h3 id="performance">Performance Considerations</h3>
126
The images on your page require possible DNS lookups, new HTTP transactions, and ultimately the transmission
127
of image data in packets over the wire. While all this is happening, the user is often left waiting for the
128
page to become fully functional. All of your <code>onload</code> JavaScript, for example, is deferred until
129
after all the page's images have finished loading.
132
Should the user have to wait for all of these images? If the images are front and center on the page, then yes,
133
suffering the load time is necessary. But what about images that the user doesn't see right away — the images
134
below the fold; the images hidden towards the end of a carousel; the images that won't appear until a non-default
135
tab of some module is clicked? ImageLoader allows you to delay the load of these images until after page load so
136
that the page is fully functional more quickly. And, by using triggers, you can ensure that the images are loaded
137
just before the user needs them so that there's no degradation of user experience.
140
<h3 id="approach">Approach</h3>
142
How can you anticipate when the user will be able to see images? Well, you as a developer know your page,
143
and you know what actions are available to the user. You can utilize your knowledge to identify user events
144
that indicate what the user is about to see.
147
For example, you know that any image lying below the fold won't be visible until the user either scrolls
148
the page or resizes the browser window. In a tabbed module, you know that the user can't click one of the
149
tabs until she mouses over that tab.
152
Consequently, you can use scroll events, mouseover events, or other indicators of user intent to stay one
153
step ahead of the user and decide when to load images. The ImageLoader Utility lets you do exactly this.
156
<h3 id="triggers">Triggers</h3>
158
Images are grouped together in terms of which user action(s) should trigger their loading.
159
A trigger is simply any DOM event. Your first step is to create an ImageLoader group object and define its trigger:
161
<pre class="code prettyprint">var myFirstGroup = new Y.ImgLoadGroup({
164
myFirstGroup.addTrigger('#someDivId', 'mouseover');</pre>
168
The <code>timeLimit</code> attribute is a time limit for the group.
169
If the user has not performed the trigger event within the specified time limit, the images are fetched anyway.
170
You can elect to not specify either the time limit or the trigger (indicating the user must perform the trigger event,
171
or there should only be a simple time delay, respectively.)
174
You can have as many triggers as you wish for a group.
175
Just add them with the <code>group</code> class's <code>addTrigger</code> method:
177
<pre class="code prettyprint">myFirstGroup.addTrigger('#someOtherDivId', 'click');</pre>
181
The trigger conditionals are disjunctive; the first one to fire initiates the image fetching.
184
<h3 id="customtriggers">Custom Event Triggers</h3>
186
You can also specify custom events as triggers. If the event belongs to the <code>Y</code> instance,
187
call <code>addCustomTrigger</code> with the event name:
189
<pre class="code prettyprint">myFirstGroup.addCustomTrigger('mycustomevent:imgloadevent');</pre>
193
Alternatively, if you have a custom event attached to a local object, you can specify this in the
194
<code>addCustomTrigger</code> call:
196
<pre class="code prettyprint">myFirstGroup.addCustomTrigger('mycustomevent:imgloadevent2', myCustomEvent);</pre>
200
And this group's images will be fetched upon <code>myCustomEvent.fire('mycustomevent:imgloadevent2');</code>.
203
<h3 id="addimages">Adding Images</h3>
204
<p>Once a group is created, you can add as many images as you'd like to it. There are three types of images:</p>
207
<strong>background image</strong> (a <code><div></code> with a background image; URL set in <code>style.backgroundImage</code>).
208
Use the <code>bgUrl</code> attribute to register this kind of image.
211
<strong>source image</strong> (an <code><img></code> tag; URL set in a <code>url</code> attribute).
212
Use the <code>srcUrl</code> attribute to register this kind of image.
215
<strong>png background image</strong> (a <code><div></code> with a png background image; for IE6,
216
sets an alpha filter <code>src</code>; for other browsers sets a background image).
217
Use the <code>isPng</code> and <code>bgUrl</code> attributes to register this kind of image.
221
To add an image to a group, register the DOM ID of the image element and the image URL with the
222
<code>registerImage</code> method:
224
<pre class="code prettyprint">myFirstGroup.registerImage({
225
domId: 'idOfDivWaitingForImage',
226
bgUrl: 'http://www.example.com/image/url'
228
myFirstGroup.registerImage({
229
domId: 'idOfImgWaitingForImage',
230
srcUrl: 'http://www.example.com/other/image/url'
232
myFirstGroup.registerImage({
233
domId: 'idOfDivWaitingForPngImage',
234
bgUrl: 'http://www.example.com/png/image/url',
240
This will set the image at <code>http://www.example.com/image/url</code> as the background-image of
241
the <code><div></code> with the ID <code>idOfDivWaitingForImage</code> and likewise with the two other image elements.
244
<h3 id="fold">Loading Images Below the Fold</h3>
246
A group can check its images at the DOM ready state and immediately begin loading those that are above the fold
247
(i.e., inside the current viewport) while delaying the load of those that aren't. Just set a value for
248
the <code>foldDistance</code> property of the group. Images are checked and loaded in a cascading fashion.
249
That is, each image will be loaded only when it comes within <code>foldDistance</code> pixels of the bottom of the viewport.
250
The effect is that images are loaded as needed as the user scrolls down the page. When you set a <code>foldDistance</code>,
251
the group automatically gets window <code>scroll</code> and window <code>resize</code> triggers.
252
<pre class="code prettyprint">var foldGroup = new Y.ImgLoadGroup({
255
foldGroup.registerImage({
256
domId: 'partwayDownPageImage',
257
bgUrl: 'http://www.example.com/image/url'
262
<h3 id="visibility">Image Visibility</h3>
264
You can set your <code><img></code> tags to have the CSS property <code>visibility:hidden</code>.
265
This will allow your page to keep its structure until the image is actually loaded. Since these images are probably
266
out of the viewport anyway, this may not make a perceptible difference, but it will help some browsers avoid reflowing
267
the page when deferred images are loaded. To accomplish this using ImageLoader, set the <code>setVisible</code>
268
attribute of the image to <code>true</code> when you register the image; ImageLoader will then set the <code>visibility</code>
269
property to <code>visible</code> when the image is fetched.
272
<h3 id="classnames">Using Class Names</h3>
274
As an alternative to registering each image with a group, you can use CSS class names to group images together.
275
When using this approach, images that are part of the same group should all share a common class name.
276
Each should also have its image set as the element's background image via CSS in the element's <code>style</code> attribute.
277
To prevent the image from loading immediately when the element renders, create a CSS style definition for that
278
class overriding the background image to <code>none</code>. Lastly, set the <code>className</code> attribute of the
282
The following combination of CSS, HTML, and JavaScript illustrates this approach:
283
<pre class="code prettyprint"><div
284
class='yui-imgload-somegroup'
285
style='background-image:url("http://www.example.com/image/url");'>
289
.yui-imgload-somegroup {
290
background:none !important;
295
someGroup.set('className', 'yui-imgload-somegroup');
296
</script></pre>
301
<h2 id="caveats">Important Usage Requirements</h2>
304
There are some important things to keep in mind while using the ImageLoader Utility.
305
Otherwise it may not work the way you expect, or it may have some undesired side effects.
308
<h3 id="src-attribute-of-source-images">"src" Attribute of Source Images</h3>
310
When using ImageLoader with <code><img></code> elements (via the <code>srcUrl</code> attribute),
311
leave the <code>src</code> attribute out of the HTML element altogether. Do not set an empty string for
312
the value of <code>src</code>. Some browsers react to this by assuming the empty string means "/", and
313
consequently the browser re-requests the current HTML page and tries to stuff it into the <code><img></code> element.
314
This is bad news for performance.
318
<img id="anImgEl" />
320
<strike><img id="anImgEl" src="" /></strike>
324
<h3 id="resizing-images">Resizing Images</h3>
326
When resizing <code><img></code> elements (via <code>height</code> and <code>width</code> attributes) on the fly,
327
the height and width of the image must be specified in the JavaScript. Do this by setting <code>width</code>/<code>height</code>
328
attributes in the <code>registerImage</code> call. Failure to do this will result in no resizing. Browsers ignore width/height
329
set in the HTML when there is no <code>src</code> attribute. And when the <code>src</code> is finally set, the width/height end
330
up being the image's natural size.
334
someGroup.registerImage({ domId: "someImgDiv", srcUrl: "http://www.example.com/image/url", width: W, height: H });
336
<strike>someGroup.registerImage({ domId: "someImgDiv", srcUrl: "http://www.example.com/image/url" });</strike>
342
<div id="sidebar" class="yui3-u">
344
<div id="toc" class="sidebox">
346
<h2 class="no-toc">Table of Contents</h2>
352
<a href="#getting-started">Getting Started</a>
355
<a href="#creating-an-image-group">Creating an Image Group</a>
358
<a href="#adding-images-to-the-group">Adding Images to the Group</a>
363
<a href="#using">Using the ImageLoader Utility</a>
366
<a href="#performance">Performance Considerations</a>
369
<a href="#approach">Approach</a>
372
<a href="#triggers">Triggers</a>
375
<a href="#customtriggers">Custom Event Triggers</a>
378
<a href="#addimages">Adding Images</a>
381
<a href="#fold">Loading Images Below the Fold</a>
384
<a href="#visibility">Image Visibility</a>
387
<a href="#classnames">Using Class Names</a>
392
<a href="#caveats">Important Usage Requirements</a>
395
<a href="#src-attribute-of-source-images">"src" Attribute of Source Images</a>
398
<a href="#resizing-images">Resizing Images</a>
408
<div class="sidebox">
410
<h2 class="no-toc">Examples</h2>
414
<ul class="examples">
417
<li data-description="Demonstrates the basic features and operation of the ImageLoader Utility, deferring the loading of images until specific events happen or specific time limits expire.">
418
<a href="basic-features.html">Basic Features of the ImageLoader Utility</a>
423
<li data-description="Loading images above the fold immediately while deferring the loading of images below the fold.">
424
<a href="below-fold.html">Loading Images Below the Fold</a>
429
<li data-description="Using CSS class names to target specific images for deferred loading.">
430
<a href="imageloader-class-names.html">Using ImageLoader with CSS Class Names</a>
444
<script src="../assets/vendor/prettify/prettify-min.js"></script>
445
<script>prettyPrint();</script>