5
<title>Example: Using ImageLoader with CSS Class Names</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: Using ImageLoader with CSS Class Names</h1>
19
<div id="main" class="yui3-u">
20
<div class="content"><div class="intro">
23
The <a href="http://developer.yahoo.com/yui/imageloader/">ImageLoader Utility</a> allows you an alternate method of
24
using CSS class names to load images.
28
Hover over each image to show its triggers and limit. Try tripping the triggers to see the load reactions.
29
Refresh the page to reset the images.
35
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== -->
39
.everything { position:relative; height:420px; }
40
.everything div { border:1px solid #888; }
41
.topmain { position:absolute; top:10px; left:120px; height:75px; width:100px; }
42
.duo1 { position:absolute; top:130px; left:20px; height:67px; width:100px; }
43
.duo2 { position:absolute; top:130px; left:220px; height:53px; width:100px; }
44
.scroll { position:absolute; top:320px; left:120px; height:72px; width:100px; }
46
.yui3-imgload-maingroup,
47
.yui3-imgload-duogroup,
48
.yui3-imgload-scrollgroup { background:none !important; }
53
<div class='everything' id='everything'>
54
<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>
55
<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>
56
<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>
57
<div class='scroll' title='group 3; scroll; no time limit'>
58
<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' />
64
YUI({filter:"debug", logInclude: {"imageloader":true, "example":true}}).use("imageloader", function(Y) {
66
var mainGroup = new Y.ImgLoadGroup({ name: 'group 1', timeLimit: 2, className: 'yui3-imgload-maingroup' });
67
mainGroup.addTrigger('#topmain', 'mouseover');
69
var duoGroup = new Y.ImgLoadGroup({ name: 'group 2', timeLimit: 4, className: 'yui3-imgload-duogroup' });
70
duoGroup.addTrigger('#duo1', 'mouseover').addTrigger('#duo2', 'click');
72
var scrollGroup = new Y.ImgLoadGroup({ name: 'group 3', className: 'yui3-imgload-scrollgroup' });
73
scrollGroup.addTrigger(window, 'scroll');
79
<!--END SOURCE CODE FOR EXAMPLE =============================== -->
82
<h2>Using CSS Class Names to Load Images</h2>
85
Instead of registering specific image ids/URLs with a group, you can simply tag the group with a CSS class.
86
The group will later use this class name to identify which DOM elements belong to the group.
87
Each group should have one corresponding class. Each class must have a <code>background:none</code> CSS definition
88
at the top of the page, as in this example:
90
<pre class="code prettyprint">.yui3-imgload-maingroup,
91
.yui3-imgload-duogroup,
92
.yui3-imgload-scrollgroup{
93
background:none !important;
97
<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>
98
<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>
99
<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>
100
<div class='scroll'>
101
<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' />
102
</div></pre>
107
A few things to note. First, the images have class names matching those in the style definitions above.
111
Second, the image URL is set in the <code>background-image</code> of the elements. The <code>background:none</code>
112
defined earlier in the CSS will be removed by the ImageLoader Utility JavaScript when the images are eventually loaded.
116
Third, since the <code><img></code> element displays its images through the <code>background-image</code>,
117
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.
118
And since that gives the image a substantial size, the browsers would show a missing-image icon if the <code>src</code>
119
attribute were not specified. Therefore we need to set one; a transparent one so that the background image will show through.
123
This brings up an important limitation with this approach: you cannot alter the natural size of the image.
124
Because the image is displayed as a background image, the browser will not resize the image according to the
125
<code>width</code>/<code>height</code> of the <code><img></code> element.
129
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.
130
All each group needs to know is the CSS class name that will identify the images.
133
<pre class="code prettyprint">var mainGroup = new Y.ImgLoadGroup({ name: 'group 1', timeLimit: 2, className: 'yui-imgload-maingroup' });
134
mainGroup.addTrigger('#topmain', 'mouseover');
136
var duoGroup = new Y.ImgLoadGroup({ name: 'group 2', timeLimit: 4, className: 'yui-imgload-duogroup' });
137
duoGroup.addTrigger('#duo1', 'mouseover').addTrigger('#duo2', 'click');
139
var scrollGroup = new Y.ImgLoadGroup({ name: 'group 3', className: 'yui-imgload-scrollgroup' });
140
scrollGroup.addTrigger(window, 'scroll');</pre>
144
Note that you are free to combine this class-name approach with the other.
145
The same group can have some images identified by class name and others by registering ids/URLs.
149
<div id="sidebar" class="yui3-u">
153
<div class="sidebox">
155
<h2 class="no-toc">Examples</h2>
159
<ul class="examples">
162
<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.">
163
<a href="basic-features.html">Basic Features of the ImageLoader Utility</a>
168
<li data-description="Loading images above the fold immediately while deferring the loading of images below the fold.">
169
<a href="below-fold.html">Loading Images Below the Fold</a>
174
<li data-description="Using CSS class names to target specific images for deferred loading.">
175
<a href="imageloader-class-names.html">Using ImageLoader with CSS Class Names</a>
189
<script src="../assets/vendor/prettify/prettify-min.js"></script>
190
<script>prettyPrint();</script>