|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Using ImageLoader with CSS Class Names</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: Using ImageLoader with CSS Class Names</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 |
The <a href="http://developer.yahoo.com/yui/imageloader/">ImageLoader Utility</a> allows you an alternate method of |
|
|
32 |
using CSS class names to load images. |
|
|
33 |
</p> |
|
|
34 |
|
|
|
35 |
<p> |
|
|
36 |
Hover over each image to show its triggers and limit. Try tripping the triggers to see the load reactions. |
|
|
37 |
Refresh the page to reset the images. |
|
|
38 |
</p> |
|
|
39 |
|
|
|
40 |
</div> |
|
|
41 |
|
|
|
42 |
<div class="example"> |
|
|
43 |
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
44 |
|
|
|
45 |
<style> |
|
|
46 |
|
|
|
47 |
.everything { position:relative; height:420px; } |
|
|
48 |
.everything div { border:1px solid #888; } |
|
|
49 |
.topmain { position:absolute; top:10px; left:120px; height:75px; width:100px; } |
|
|
50 |
.duo1 { position:absolute; top:130px; left:20px; height:67px; width:100px; } |
|
|
51 |
.duo2 { position:absolute; top:130px; left:220px; height:53px; width:100px; } |
|
|
52 |
.scroll { position:absolute; top:320px; left:120px; height:72px; width:100px; } |
|
|
53 |
|
|
|
54 |
.yui3-imgload-maingroup, |
|
|
55 |
.yui3-imgload-duogroup, |
|
|
56 |
.yui3-imgload-scrollgroup { background:none !important; } |
|
|
57 |
|
|
|
58 |
</style> |
|
|
59 |
|
|
|
60 |
|
|
|
61 |
<div class='everything' id='everything'> |
|
|
62 |
<div class='topmain yui3-imgload-maingroup' id='topmain' style='background-image:url("http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/museum.jpg");' title='group 1; mouseover image; 2 sec limit'></div> |
|
|
63 |
<div class='duo1 yui3-imgload-duogroup' id='duo1' style='background-image:url("http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/uluru.jpg");' title='group 2; mouseover left image, or click on right image; 4 sec limit'></div> |
|
|
64 |
<div class='duo2 yui3-imgload-duogroup' id='duo2' style='background-image:url("http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/katatjuta.jpg");' title='group 2; mouseover left image, or click on right image; 4 sec limit'></div> |
|
|
65 |
<div class='scroll' title='group 3; scroll; no time limit'> |
|
|
66 |
<img id='scrollImg' class='yui3-imgload-scrollgroup' style='background-image:url("http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/morraine.jpg");' src='http://l.yimg.com/a/i/us/tr/b/1px_trans.gif' width='100' height='72' /> |
|
|
67 |
</div> |
|
|
68 |
</div> |
|
|
69 |
|
|
|
70 |
<script> |
|
|
71 |
|
|
|
72 |
YUI({filter:"debug", logInclude: {"imageloader":true, "example":true}}).use("imageloader", function(Y) { |
|
|
73 |
|
|
|
74 |
var mainGroup = new Y.ImgLoadGroup({ name: 'group 1', timeLimit: 2, className: 'yui3-imgload-maingroup' }); |
|
|
75 |
mainGroup.addTrigger('#topmain', 'mouseover'); |
|
|
76 |
|
|
|
77 |
var duoGroup = new Y.ImgLoadGroup({ name: 'group 2', timeLimit: 4, className: 'yui3-imgload-duogroup' }); |
|
|
78 |
duoGroup.addTrigger('#duo1', 'mouseover').addTrigger('#duo2', 'click'); |
|
|
79 |
|
|
|
80 |
var scrollGroup = new Y.ImgLoadGroup({ name: 'group 3', className: 'yui3-imgload-scrollgroup' }); |
|
|
81 |
scrollGroup.addTrigger(window, 'scroll'); |
|
|
82 |
|
|
|
83 |
}); |
|
|
84 |
|
|
|
85 |
</script> |
|
|
86 |
|
|
|
87 |
<!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
88 |
</div> |
|
|
89 |
|
|
|
90 |
<h2>Using CSS Class Names to Load Images</h2> |
|
|
91 |
|
|
|
92 |
<p> |
|
|
93 |
Instead of registering specific image ids/URLs with a group, you can simply tag the group with a CSS class. |
|
|
94 |
The group will later use this class name to identify which DOM elements belong to the group. |
|
|
95 |
Each group should have one corresponding class. Each class must have a <code>background:none</code> CSS definition |
|
|
96 |
at the top of the page, as in this example: |
|
|
97 |
<h5>CSS</h5> |
|
|
98 |
<pre class="code prettyprint">.yui3-imgload-maingroup, |
|
|
99 |
.yui3-imgload-duogroup, |
|
|
100 |
.yui3-imgload-scrollgroup{ |
|
|
101 |
background:none !important; |
|
|
102 |
}</pre> |
|
|
103 |
|
|
|
104 |
<h5>HTML</h5> |
|
|
105 |
<pre class="code prettyprint"><div class='topmain yui-imgload-maingroup' id='topmain' style='background-image:url("http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/museum.jpg");'></div> |
|
|
106 |
<div class='duo1 yui-imgload-duogroup' id='duo1' style='background-image:url("http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/uluru.jpg");'></div> |
|
|
107 |
<div class='duo2 yui-imgload-duogroup' id='duo2' style='background-image:url("http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/katatjuta.jpg");'></div> |
|
|
108 |
<div class='scroll'> |
|
|
109 |
<img id='scrollImg' class='yui-imgload-scrollgroup' style='background-image:url("http://developer.yahoo.com/yui/docs/assets/examples/exampleimages/small/morraine.jpg");' src='http://l.yimg.com/a/i/us/tr/b/1px_trans.gif' width='100' height='72' /> |
|
|
110 |
</div></pre> |
|
|
111 |
|
|
|
112 |
</p> |
|
|
113 |
|
|
|
114 |
<p> |
|
|
115 |
A few things to note. First, the images have class names matching those in the style definitions above. |
|
|
116 |
</p> |
|
|
117 |
|
|
|
118 |
<p> |
|
|
119 |
Second, the image URL is set in the <code>background-image</code> of the elements. The <code>background:none</code> |
|
|
120 |
defined earlier in the CSS will be removed by the ImageLoader Utility JavaScript when the images are eventually loaded. |
|
|
121 |
</p> |
|
|
122 |
|
|
|
123 |
<p> |
|
|
124 |
Third, since the <code><img></code> element displays its images through the <code>background-image</code>, |
|
|
125 |
its size won't change when the image is loaded. Therefore the <code>width</code>/<code>height</code> needs to be set in the HTML. |
|
|
126 |
And since that gives the image a substantial size, the browsers would show a missing-image icon if the <code>src</code> |
|
|
127 |
attribute were not specified. Therefore we need to set one; a transparent one so that the background image will show through. |
|
|
128 |
</p> |
|
|
129 |
|
|
|
130 |
<p> |
|
|
131 |
This brings up an important limitation with this approach: you cannot alter the natural size of the image. |
|
|
132 |
Because the image is displayed as a background image, the browser will not resize the image according to the |
|
|
133 |
<code>width</code>/<code>height</code> of the <code><img></code> element. |
|
|
134 |
</p> |
|
|
135 |
|
|
|
136 |
<p> |
|
|
137 |
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. |
|
|
138 |
All each group needs to know is the CSS class name that will identify the images. |
|
|
139 |
</p> |
|
|
140 |
|
|
|
141 |
<pre class="code prettyprint">var mainGroup = new Y.ImgLoadGroup({ name: 'group 1', timeLimit: 2, className: 'yui-imgload-maingroup' }); |
|
|
142 |
mainGroup.addTrigger('#topmain', 'mouseover'); |
|
|
143 |
|
|
|
144 |
var duoGroup = new Y.ImgLoadGroup({ name: 'group 2', timeLimit: 4, className: 'yui-imgload-duogroup' }); |
|
|
145 |
duoGroup.addTrigger('#duo1', 'mouseover').addTrigger('#duo2', 'click'); |
|
|
146 |
|
|
|
147 |
var scrollGroup = new Y.ImgLoadGroup({ name: 'group 3', className: 'yui-imgload-scrollgroup' }); |
|
|
148 |
scrollGroup.addTrigger(window, 'scroll');</pre> |
|
|
149 |
|
|
|
150 |
|
|
|
151 |
<p> |
|
|
152 |
Note that you are free to combine this class-name approach with the other. |
|
|
153 |
The same group can have some images identified by class name and others by registering ids/URLs. |
|
|
154 |
</p></div> |
|
|
155 |
</div> |
|
|
156 |
</div> |
|
|
157 |
|
|
|
158 |
<div class="yui3-u-1-4"> |
|
|
159 |
<div class="sidebar"> |
|
|
160 |
|
|
|
161 |
|
|
|
162 |
|
|
|
163 |
<div class="sidebox"> |
|
|
164 |
<div class="hd"> |
|
|
165 |
<h2 class="no-toc">Examples</h2> |
|
|
166 |
</div> |
|
|
167 |
|
|
|
168 |
<div class="bd"> |
|
|
169 |
<ul class="examples"> |
|
|
170 |
|
|
|
171 |
|
|
|
172 |
<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."> |
|
|
173 |
<a href="basic-features.html">Basic Features of the ImageLoader Utility</a> |
|
|
174 |
</li> |
|
|
175 |
|
|
|
176 |
|
|
|
177 |
|
|
|
178 |
<li data-description="Loading images above the fold immediately while deferring the loading of images below the fold."> |
|
|
179 |
<a href="below-fold.html">Loading Images Below the Fold</a> |
|
|
180 |
</li> |
|
|
181 |
|
|
|
182 |
|
|
|
183 |
|
|
|
184 |
<li data-description="Using CSS class names to target specific images for deferred loading."> |
|
|
185 |
<a href="imageloader-class-names.html">Using ImageLoader with CSS Class Names</a> |
|
|
186 |
</li> |
|
|
187 |
|
|
|
188 |
|
|
|
189 |
</ul> |
|
|
190 |
</div> |
|
|
191 |
</div> |
|
|
192 |
|
|
|
193 |
|
|
|
194 |
|
|
|
195 |
</div> |
|
|
196 |
</div> |
|
|
197 |
</div> |
|
|
198 |
</div> |
|
|
199 |
|
|
|
200 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
201 |
<script>prettyPrint();</script> |
|
|
202 |
|
|
|
203 |
<script> |
|
|
204 |
YUI.Env.Tests = { |
|
|
205 |
examples: [], |
|
|
206 |
project: '../assets', |
|
|
207 |
assets: '../assets/imageloader', |
|
|
208 |
name: 'imageloader-class-names', |
|
|
209 |
title: 'Using ImageLoader with CSS Class Names', |
|
|
210 |
newWindow: '', |
|
|
211 |
auto: false |
|
|
212 |
}; |
|
|
213 |
YUI.Env.Tests.examples.push('basic-features'); |
|
|
214 |
YUI.Env.Tests.examples.push('below-fold'); |
|
|
215 |
YUI.Env.Tests.examples.push('imageloader-class-names'); |
|
|
216 |
|
|
|
217 |
</script> |
|
|
218 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
219 |
|
|
|
220 |
|
|
|
221 |
|
|
|
222 |
</body> |
|
|
223 |
</html> |