YUI Library Home

YUI Library Examples: Carousel Control: Using deferred loading of items and Carousel's built-in paginator

Carousel Control: Using deferred loading of items and Carousel's built-in paginator

This example extends Using Ajax for deferred loading of items and demonstrates how to support YUI Carousel Control's built-in paginator via the replaceItem method.

Demonstrates how to enable deferred loading of items and Carousel's own paginator

Here we will use YUI Carousel Control's loadItems event and replaceItem method to dynamically load any one of Carousel's pages on the fly with Carousel's built-in paginator.

This example has the following dependencies:

1<link type="text/css" rel="stylesheet" href="http://yui.yahooapis.com/2.8.1/build/carousel/assets/skins/sam/carousel.css"
2<script src="http://yui.yahooapis.com/2.8.1/build/yahoo/yahoo-dom-event.js"></script> 
3<script src="http://yui.yahooapis.com/2.8.1/build/element/element-min.js"></script> 
4<script src="http://yui.yahooapis.com/2.8.1/build/connection/connection-min.js"></script> 
5<script src="http://yui.yahooapis.com/2.8.1/build/carousel/carousel-min.js"></script> 
view plain | print | ?

Initially we use YUI Connection Manager to load the initial set of items as soon as part of the DOM is visible.

1<div id="container"><ol id="carousel"></ol> 
2</div> 
3<!-- The spotlight container --> 
4<div id="spotlight"></div> 
view plain | print | ?

We have a required CSS rule to set the dimensions for the Carousel items.

1/* Always be sure to give your carousel items a width and a height */ 
2.yui-carousel-element li { 
3    height75px
4    width75px
5} 
view plain | print | ?

And, as we'll see later, since we're increasing the default number of UI pager buttons which will be displayed, we increase the amount of space required to display the 6th page button, in IE6 and IE7:

1/* Since for this example, we display more than the default 5 page buttons,
2   by setting MAX_PAGER_BUTTONS, we increase the offset margin for IE6/7 to make space for the 6th button */ 
3.yui-skin-sam .yui-carousel-nav ul { 
4    *margin-left-190px
5} 
view plain | print | ?

The YUI Carousel Control's constructor is passed numItems (the total number of items) so that it triggers the loadItems event if the items are not available. We'll use Connection Manager to load a set of items into the Carousel as early as possible.

For a more traditional paginator, we increase the MAX_PAGER_BUTTONS property (default is 5 pages, this example has 100), which causes Carousel to render a paginator comprised of a row of anchors in leu of a select box.

1YAHOO.util.Event.onDOMReady(function (ev) { 
2 
3    var carousel, spotlight = YAHOO.util.Dom.get("spotlight"); 
4 
5    /* Allow for anchor pagination to better demonstrate backwards loading. 
6       Note: MAX_PAGER_BUTTONS defaults to 5 making a Carousel with more than
7       5 pages spawn a select box containing pages instead of page anchors. */ 
8 
9    YAHOO.widget.Carousel.prototype.CONFIG.MAX_PAGER_BUTTONS = 100; 
10 
11    carousel = new YAHOO.widget.Carousel("container", { 
12        /* Setting numItems is required for dynamic loading. This property lets Carousel 
13           know how many total items it will ever be populated with.
14           Not to be confused with numVisible: the number of items to display
15           per page. In this case we don't specify it and Carousel 
16           defaults to 3 items visible per page. */ 
17 
18        numItems: 17 
19    }); 
20 
21    batchSize = carousel.get("numVisible"); 
22 
23    YAHOO.util.Connect.asyncRequest("GET""assets/php/getimages2.php" + "?r="+Math.random()+"&batchSize=" + batchSize, { 
24        success: function (o) { 
25            var i, r = eval("(" + o.responseText + ")"); 
26 
27            curpos = r.length; 
28 
29            for (i = 0; i < curpos; i++) { 
30                items.push(r[i]); 
31            } 
32 
33            // check if the Carousel widget is available 
34            if (typeof carousel != "undefined") { 
35                for (i = 0; i < curpos; i++) { 
36                    // if so, add each item 
37                    carousel.addItem(getImageTag(items[i])); 
38                } 
39                carousel.set("selectedItem", 0); 
40                items = []; 
41            } 
42        }, 
43 
44        failure: function (o) { 
45            alert("Ajax request failed!"); 
46        } 
47    }); 
48 
49    carousel.on("loadItems"function (o) { 
50        // more items available? 
51        getImages.call(this, o); 
52    }); 
53 
54    carousel.on("itemSelected"function (index) { 
55        // item has the reference to the Carousel\'s item 
56        var item = carousel.getElementForItem(index); 
57 
58        if (item) { 
59            spotlight.innerHTML = "<img src=\""+getLargeImage(item)+"\">"
60        } 
61    }); 
62}); 
view plain | print | ?

Triggered by the loadItems event, the getImages() function sends a request to our web service specifying the current item selected (curpos) and the amount of images to return (batchSize). If the request is a success, the image is extracted from the return data and Carousel is populated accordingly: if mock loading items are detected on the previous page (inserted as placeholders after a user skips one or more pages that have never been loaded), the replaceItem method is invoked, which swaps out each placeholder with its respective image; alternatively, the addItem method is called when a user navigates successively through pages.

1var curpos, batchSize, items = []; 
2function getImages(o) { 
3    var carousel = this
4        revealEnabled = carousel.get("revealAmount"), 
5        numVisible = carousel.get("numVisible"), 
6        curpos = o.num > numVisible + (revealEnabled?1:0) ? o.last - (o.last % numVisible) : o.first; 
7            
8    YAHOO.util.Connect.asyncRequest("GET""'.$webService.'""?r="+Math.random()+"&batchSize="+ batchSize + "&pos=" + curpos, 
9            { 
10                success: function (o) { 
11                    var i = curpos, 
12                        j = 0, 
13                        r = eval("(" + o.responseText + ")"), 
14                        item,  
15                        loadingItems; 
16                         
17                    curpos += r.length; 
18 
19                    while (i < curpos) { 
20                         
21                        if (r[j]) { 
22                           
23                          item = getImageTag(r[j]);  
24 
25                          loadingItems = carousel.getLoadingItems(); 
26 
27                          if(loadingItems[i]){ 
28                               // use replaceItem to swap out each placeholder item on the previous page and allow for backwards navigation  
29                               carousel.replaceItem(item,i); 
30                           } else { 
31                               carousel.addItem(item); 
32                           } 
33                             
34                        } else { 
35                            break
36                        } 
37                        i++; 
38                        j++; 
39                    } 
40 
41                    carousel.set("selectedItem", carousel.get("firstVisible")); 
42                }, 
43 
44                failure: function (o) { 
45                    alert("Ajax request failed!"); 
46                } 
47    }); 
48
view plain | print | ?

Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

Copyright © 2010 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings