|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Loading Images Below the Fold</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<h1>Example: Loading Images Below the Fold</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><div class="intro"> |
|
|
29 |
|
|
|
30 |
<p> |
|
|
31 |
Often pages will have a number of images below the fold, hidden from the user's view. |
|
|
32 |
These are prime candidates to load with the <a href="http://developer.yahoo.com/yui/imageloader/">ImageLoader Utility</a>. |
|
|
33 |
</p> |
|
|
34 |
|
|
|
35 |
<p> |
|
|
36 |
All the images in this example belong to the same group, and each loads immediately only when it appears above, |
|
|
37 |
or within the specified distance (25px) of, the page fold. |
|
|
38 |
</p> |
|
|
39 |
|
|
|
40 |
</div> |
|
|
41 |
|
|
|
42 |
<div class="example"> |
|
|
43 |
<!--BEGIN SOURCE CODE FOR EXAMPLE ===============================--> |
|
|
44 |
|
|
|
45 |
<style> |
|
|
46 |
.everything .cont { border:1px solid #888; width:100px; margin:75px 50px; } |
|
|
47 |
.everything .rightCol { margin-left:300px; } |
|
|
48 |
#img1Cont { height:75px; margin-top:25px; } |
|
|
49 |
#img2Cont { height:67px; } |
|
|
50 |
#img3Cont { height:53px; } |
|
|
51 |
#img4Cont { height:72px; } |
|
|
52 |
#img5Cont { height:75px; margin-bottom:25px; } |
|
|
53 |
</style> |
|
|
54 |
|
|
|
55 |
|
|
|
56 |
<div class='everything' id='everything'> |
|
|
57 |
<div class='cont' id='img1Cont'> |
|
|
58 |
<img id='img1' /> |
|
|
59 |
</div> |
|
|
60 |
<div class='cont rightCol' id='img2Cont'> |
|
|
61 |
<img id='img2' /> |
|
|
62 |
</div> |
|
|
63 |
<div class='cont' id='img3Cont'> |
|
|
64 |
<img id='img3' /> |
|
|
65 |
|
|
|
66 |
</div> |
|
|
67 |
<div class='cont rightCol' id='img4Cont'> |
|
|
68 |
<img id='img4' /> |
|
|
69 |
</div> |
|
|
70 |
<div class='cont' id='img5Cont'> |
|
|
71 |
<img id='img5' /> |
|
|
72 |
</div> |
|
|
73 |
</div> |
|
|
74 |
|
|
|
75 |
|
|
|
76 |
<script> |
|
|
77 |
|
|
|
78 |
YUI({filter:"debug", logInclude: {"imageloader":true, "example":true}}).use("imageloader", function(Y) { |
|
|
79 |
|
|
|
80 |
var foldGroup = new Y.ImgLoadGroup({ name: 'fold group', foldDistance: 25 }); |
|
|
81 |
foldGroup.registerImage({ domId: 'img1', srcUrl: 'http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/museum.jpg' }); |
|
|
82 |
foldGroup.registerImage({ domId: 'img2', srcUrl: 'http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/uluru.jpg' }); |
|
|
83 |
foldGroup.registerImage({ domId: 'img3', srcUrl: 'http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/katatjuta.jpg' }); |
|
|
84 |
foldGroup.registerImage({ domId: 'img4', srcUrl: 'http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/morraine.jpg' }); |
|
|
85 |
foldGroup.registerImage({ domId: 'img5', srcUrl: 'http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/japan.jpg' }); |
|
|
86 |
|
|
|
87 |
/* |
|
|
88 |
var foldGroup = new YAHOO.util.ImageLoader.group(window, 'scroll'); |
|
|
89 |
foldGroup.foldConditional = true; |
|
|
90 |
//foldGroup.addTrigger(window, 'resize'); |
|
|
91 |
foldGroup.name = 'fold_group'; |
|
|
92 |
*/ |
|
|
93 |
|
|
|
94 |
/* |
|
|
95 |
* This uncustomary wait before adding the resize trigger is done specifically to cater to IE for this example. |
|
|
96 |
* 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. |
|
|
97 |
* This 200 ms delay allows IE to render the Logger without interference. |
|
|
98 |
* In your code, you would add the resize trigger in a straightforward fashion, as is documented in the example. |
|
|
99 |
*/ |
|
|
100 |
//YAHOO.util.Event.addListener(window, 'load', function() { setTimeout("foldGroup.addTrigger(window, 'resize')", 200); }); |
|
|
101 |
|
|
|
102 |
}); |
|
|
103 |
|
|
|
104 |
</script> |
|
|
105 |
|
|
|
106 |
<!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
107 |
|
|
|
108 |
</div> |
|
|
109 |
|
|
|
110 |
<h2>Loading Images Below the Fold</h2> |
|
|
111 |
|
|
|
112 |
<p> |
|
|
113 |
You can easily have images load immediately if they are above the fold while delaying the load of images below the fold. |
|
|
114 |
This saves you from loading any images that the user can't see because they are beyond her browser's viewable area. |
|
|
115 |
</p> |
|
|
116 |
|
|
|
117 |
<p> |
|
|
118 |
All we need is one group, and we specify its <code>foldDistance</code> attribute to <code>25</code> (25px). |
|
|
119 |
Any group with this attribute set will, at the DOM ready state, examine the page coordinates of all images registered |
|
|
120 |
to that group. Any images located above the fold, or no farther than the specified distance below the fold, will load immediately. |
|
|
121 |
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 |
|
|
122 |
to the fold. |
|
|
123 |
</p> |
|
|
124 |
|
|
|
125 |
<pre class="code prettyprint">var foldGroup = new Y.ImgLoadGroup({ name: 'fold group', foldDistance: 25 }); |
|
|
126 |
foldGroup.registerImage({ domId: 'img1', srcUrl: 'http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/museum.jpg' }); |
|
|
127 |
foldGroup.registerImage({ domId: 'img2', srcUrl: 'http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/uluru.jpg' }); |
|
|
128 |
foldGroup.registerImage({ domId: 'img3', srcUrl: 'http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/katatjuta.jpg' }); |
|
|
129 |
foldGroup.registerImage({ domId: 'img4', srcUrl: 'http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/morraine.jpg' }); |
|
|
130 |
foldGroup.registerImage({ domId: 'img5', srcUrl: 'http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/japan.jpg' });</pre> |
|
|
131 |
|
|
|
132 |
|
|
|
133 |
<p> |
|
|
134 |
When you specify a <code>foldDistance</code> value, <code>scroll</code> and <code>resize</code> triggers are added to the |
|
|
135 |
group automatically. Thus you will typically not need to set any triggers for the group explicitly. |
|
|
136 |
</p> |
|
|
137 |
|
|
|
138 |
<p> |
|
|
139 |
How do you know that the images below the fold are, in fact, not loaded immediately? There are several tools available to |
|
|
140 |
monitor the HTTP requests of your browser, including Firebug for Firefox and HTTPWatch for IE. With these tools you can |
|
|
141 |
monitor precisely when each image on a page is loaded. |
|
|
142 |
</p> |
|
|
143 |
</div> |
|
|
144 |
</div> |
|
|
145 |
</div> |
|
|
146 |
|
|
|
147 |
<div class="yui3-u-1-4"> |
|
|
148 |
<div class="sidebar"> |
|
|
149 |
|
|
|
150 |
|
|
|
151 |
|
|
|
152 |
<div class="sidebox"> |
|
|
153 |
<div class="hd"> |
|
|
154 |
<h2 class="no-toc">Examples</h2> |
|
|
155 |
</div> |
|
|
156 |
|
|
|
157 |
<div class="bd"> |
|
|
158 |
<ul class="examples"> |
|
|
159 |
|
|
|
160 |
|
|
|
161 |
<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."> |
|
|
162 |
<a href="basic-features.html">Basic Features of the ImageLoader Utility</a> |
|
|
163 |
</li> |
|
|
164 |
|
|
|
165 |
|
|
|
166 |
|
|
|
167 |
<li data-description="Loading images above the fold immediately while deferring the loading of images below the fold."> |
|
|
168 |
<a href="below-fold.html">Loading Images Below the Fold</a> |
|
|
169 |
</li> |
|
|
170 |
|
|
|
171 |
|
|
|
172 |
|
|
|
173 |
<li data-description="Using CSS class names to target specific images for deferred loading."> |
|
|
174 |
<a href="imageloader-class-names.html">Using ImageLoader with CSS Class Names</a> |
|
|
175 |
</li> |
|
|
176 |
|
|
|
177 |
|
|
|
178 |
</ul> |
|
|
179 |
</div> |
|
|
180 |
</div> |
|
|
181 |
|
|
|
182 |
|
|
|
183 |
|
|
|
184 |
</div> |
|
|
185 |
</div> |
|
|
186 |
</div> |
|
|
187 |
</div> |
|
|
188 |
|
|
|
189 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
190 |
<script>prettyPrint();</script> |
|
|
191 |
|
|
|
192 |
<script> |
|
|
193 |
YUI.Env.Tests = { |
|
|
194 |
examples: [], |
|
|
195 |
project: '../assets', |
|
|
196 |
assets: '../assets/imageloader', |
|
|
197 |
name: 'below-fold', |
|
|
198 |
title: 'Loading Images Below the Fold', |
|
|
199 |
newWindow: '', |
|
|
200 |
auto: false |
|
|
201 |
}; |
|
|
202 |
YUI.Env.Tests.examples.push('basic-features'); |
|
|
203 |
YUI.Env.Tests.examples.push('below-fold'); |
|
|
204 |
YUI.Env.Tests.examples.push('imageloader-class-names'); |
|
|
205 |
|
|
|
206 |
</script> |
|
|
207 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
208 |
|
|
|
209 |
|
|
|
210 |
|
|
|
211 |
</body> |
|
|
212 |
</html> |