5
<title>Example: Loading Images Below the Fold</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>
14
<h1>Example: Loading Images Below the Fold</h1>
19
<div id="main" class="yui3-u">
20
<div class="content"><div class="intro">
23
Often pages will have a number of images below the fold, hidden from the user's view.
24
These are prime candidates to load with the <a href="http://developer.yahoo.com/yui/imageloader/">ImageLoader Utility</a>.
28
All the images in this example belong to the same group, and each loads immediately only when it appears above,
29
or within the specified distance (25px) of, the page fold.
35
<!--BEGIN SOURCE CODE FOR EXAMPLE ===============================-->
38
.everything .cont { border:1px solid #888; width:100px; margin:75px 50px; }
39
.everything .rightCol { margin-left:300px; }
40
#img1Cont { height:75px; margin-top:25px; }
41
#img2Cont { height:67px; }
42
#img3Cont { height:53px; }
43
#img4Cont { height:72px; }
44
#img5Cont { height:75px; margin-bottom:25px; }
48
<div class='everything' id='everything'>
49
<div class='cont' id='img1Cont'>
52
<div class='cont rightCol' id='img2Cont'>
55
<div class='cont' id='img3Cont'>
59
<div class='cont rightCol' id='img4Cont'>
62
<div class='cont' id='img5Cont'>
70
YUI({filter:"debug", logInclude: {"imageloader":true, "example":true}}).use("imageloader", function(Y) {
72
var foldGroup = new Y.ImgLoadGroup({ name: 'fold group', foldDistance: 25 });
73
foldGroup.registerImage({ domId: 'img1', srcUrl: 'http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/museum.jpg' });
74
foldGroup.registerImage({ domId: 'img2', srcUrl: 'http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/uluru.jpg' });
75
foldGroup.registerImage({ domId: 'img3', srcUrl: 'http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/katatjuta.jpg' });
76
foldGroup.registerImage({ domId: 'img4', srcUrl: 'http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/morraine.jpg' });
77
foldGroup.registerImage({ domId: 'img5', srcUrl: 'http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/japan.jpg' });
80
var foldGroup = new YAHOO.util.ImageLoader.group(window, 'scroll');
81
foldGroup.foldConditional = true;
82
//foldGroup.addTrigger(window, 'resize');
83
foldGroup.name = 'fold_group';
87
* This uncustomary wait before adding the resize trigger is done specifically to cater to IE for this example.
88
* In IE and with the Logger enabled, IE fires an immediate resize event while rendering the Logger module, consequently loading all the images in the example.
89
* This 200 ms delay allows IE to render the Logger without interference.
90
* In your code, you would add the resize trigger in a straightforward fashion, as is documented in the example.
92
//YAHOO.util.Event.addListener(window, 'load', function() { setTimeout("foldGroup.addTrigger(window, 'resize')", 200); });
98
<!--END SOURCE CODE FOR EXAMPLE =============================== -->
102
<h2>Loading Images Below the Fold</h2>
105
You can easily have images load immediately if they are above the fold while delaying the load of images below the fold.
106
This saves you from loading any images that the user can't see because they are beyond her browser's viewable area.
110
All we need is one group, and we specify its <code>foldDistance</code> attribute to <code>25</code> (25px).
111
Any group with this attribute set will, at the DOM ready state, examine the page coordinates of all images registered
112
to that group. Any images located above the fold, or no farther than the specified distance below the fold, will load immediately.
113
The rest will be checked again at any <code>scroll</code> or <code>resize</code> event and be loaded only when they're near enough
117
<pre class="code prettyprint">var foldGroup = new Y.ImgLoadGroup({ name: 'fold group', foldDistance: 25 });
118
foldGroup.registerImage({ domId: 'img1', srcUrl: 'http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/museum.jpg' });
119
foldGroup.registerImage({ domId: 'img2', srcUrl: 'http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/uluru.jpg' });
120
foldGroup.registerImage({ domId: 'img3', srcUrl: 'http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/katatjuta.jpg' });
121
foldGroup.registerImage({ domId: 'img4', srcUrl: 'http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/morraine.jpg' });
122
foldGroup.registerImage({ domId: 'img5', srcUrl: 'http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/japan.jpg' });</pre>
126
When you specify a <code>foldDistance</code> value, <code>scroll</code> and <code>resize</code> triggers are added to the
127
group automatically. Thus you will typically not need to set any triggers for the group explicitly.
131
How do you know that the images below the fold are, in fact, not loaded immediately? There are several tools available to
132
monitor the HTTP requests of your browser, including Firebug for Firefox and HTTPWatch for IE. With these tools you can
133
monitor precisely when each image on a page is loaded.
138
<div id="sidebar" class="yui3-u">
142
<div class="sidebox">
144
<h2 class="no-toc">Examples</h2>
148
<ul class="examples">
151
<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.">
152
<a href="basic-features.html">Basic Features of the ImageLoader Utility</a>
157
<li data-description="Loading images above the fold immediately while deferring the loading of images below the fold.">
158
<a href="below-fold.html">Loading Images Below the Fold</a>
163
<li data-description="Using CSS class names to target specific images for deferred loading.">
164
<a href="imageloader-class-names.html">Using ImageLoader with CSS Class Names</a>
178
<script src="../assets/vendor/prettify/prettify-min.js"></script>
179
<script>prettyPrint();</script>