This example demonstrates the use of YUI Paginator Control for navigating the YUI Carousel Control.
In this example we'll use the YUI Paginator Control for navigating through the YUI Carousel Control.
This example has the following dependencies:
1 | <link type="text/css" rel="stylesheet" href="http://yui.yahooapis.com/2.7.0/build/carousel/assets/skins/sam/carousel.css"> |
2 | <script src="http://yui.yahooapis.com/2.7.0/build/yahoo/yahoo-dom-event.js"></script> |
3 | <script src="http://yui.yahooapis.com/2.7.0/build/element/element-min.js"></script> |
4 | <script src="http://yui.yahooapis.com/2.7.0/build/carousel/carousel-min.js"></script> |
5 | <script src="http://yui.yahooapis.com/2.7.0/build/paginator/paginator-min.js"></script> |
view plain | print | ? |
This example uses progressive enhancement; the Carousel is created from an ordered list. This is not a requirement.
1 | <div id="container"> |
2 | <ol> |
3 | <li> |
4 | <img src="http://farm1.static.flickr.com/135/342099636_7b05b7cde5_s.jpg"> |
5 | </li> |
6 | <li> |
7 | <img src="http://farm1.static.flickr.com/136/342099938_fdef3ca3b5_s.jpg"> |
8 | </li> |
9 | <li> |
10 | <img src="http://farm1.static.flickr.com/147/342099522_3827eaa929_s.jpg"> |
11 | </li> |
12 | <li> |
13 | <img src="http://farm1.static.flickr.com/143/342100011_ec4d338c71_s.jpg"> |
14 | </li> |
15 | <li> |
16 | <img src="http://farm1.static.flickr.com/161/342100080_0fe4f9ccb0_s.jpg"> |
17 | </li> |
18 | <li> |
19 | <img src="http://farm1.static.flickr.com/153/342100324_82589c0ebe_s.jpg"> |
20 | </li> |
21 | <li> |
22 | <img src="http://farm1.static.flickr.com/147/342100376_d0336252a7_s.jpg"> |
23 | </li> |
24 | <li> |
25 | <img src="http://farm1.static.flickr.com/156/342100472_b9bc985fa4_s.jpg"> |
26 | </li> |
27 | <li> |
28 | <img src="http://farm1.static.flickr.com/133/342100566_39dae4698f_s.jpg"> |
29 | </li> |
30 | </ol> |
31 | </div> |
view plain | print | ? |
We will add a container element where the YUI Paginator Control would place its navigation controls.
1 | <div id="pagination"></div> |
view plain | print | ? |
We will add a container element where the spotlight image is displayed.
1 | <div id="spotlight"></div> |
view plain | print | ? |
We'll have to set the dimensions of a single item in the Carousel to make it adjust the container width automatically based on the number of items to be displayed in the view port.
1 | .yui-carousel-element li { |
2 | height: 75px; |
3 | width: 75px; |
4 | } |
view plain | print | ? |
Along with the above CSS rule, we'll add some more CSS rules to style the Paginator control.
1 | #pagination { |
2 | font: 13px Arial, Helvetica, sans-serif; |
3 | margin: 10px auto; |
4 | padding: 1px; |
5 | text-align: center; |
6 | width: 240px; |
7 | } |
8 | |
9 | .yui-skin-sam .yui-pg-page { |
10 | background: #fff; |
11 | border: 1px solid #cbcbcb; |
12 | margin: 0 1px; |
13 | padding: 2px 6px; |
14 | } |
view plain | print | ? |
Since we are using the Paginator control for the navigation, we can hide the Carousel control's navigation. In addition to this, we can style the Carousel container to look pretty.
1 | .yui-carousel-nav { |
2 | display: none; |
3 | } |
4 | |
5 | #container { |
6 | border: none; |
7 | background: #e2edfa; |
8 | -moz-border-radius: 6px; |
9 | padding: 8px 16px 4px; |
10 | } |
view plain | print | ? |
Since we have the elements in place, we can invoke the Carousel's constructor to create the widget. Once the Carousel is instantiated, we can get the total number of records (items) to be used to instantiate the Paginator.
1 | YAHOO.util.Event.onDOMReady(function (ev) { |
2 | var carousel = new YAHOO.widget.Carousel("container"); |
3 | var numItems = carousel.get("numItems"); |
4 | var numVisible = carousel.get("numVisible"); |
5 | var paginator = new YAHOO.widget.Paginator({ |
6 | containers: "pagination", |
7 | rowsPerPage: 1, |
8 | template: "{PreviousPageLink} {PageLinks} {NextPageLink}", |
9 | totalRecords: Math.ceil(numItems / numVisible) |
10 | }); |
view plain | print | ? |
On clicking any of the pagination links, the Paginator control fires a changeRequest
event. We can subscribe to this event to navigate to the appropriate page in the Carousel.
1 | paginator.subscribe("changeRequest", function (state) { |
2 | // set the selectedItem so that the Carousel scrolls to the page automatically |
3 | carousel.set("selectedItem", (state.page - 1) * numVisible); |
4 | paginator.setState(state); |
5 | }); |
view plain | print | ? |
Similarly, when the user scrolls through the Carousel using a keyboard (by hitting the arrow keys), whenever the Carousel scrolls to a new page it triggers a pageChange
event. This event needs to be listened to set the Paginator to the correct state on such an event.
1 | carousel.on("pageChange", function (page) { |
2 | // Paginator's page begins from 1 |
3 | // Also, we need to suppress this triggering a changeRequest event. |
4 | paginator.setPage(page + 1, true); |
5 | }); |
view plain | print | ? |
Now, we can subscribe to the itemSelected
event that the Carousel exposes.
This event is triggered whenever an item is selected and it returns the
index of the selected item. With the index of the item, we can use the
Carousel's getElementForItem()
API to get the reference to the Carousel's item (an li
element in
our case).
1 | carousel.on("itemSelected", function (index) { |
2 | // item has the reference to the Carousel's item |
3 | var item = carousel.getElementForItem(index); |
4 | }); |
view plain | print | ? |
Once the reference to the Carousel's item is obtained, it is straightforward to implement a function that extracts the image within it.
1 | // Get the image link from within its (parent) container. |
2 | function getImage(parent) { |
3 | var el = parent.firstChild; |
4 | |
5 | while (el) { // walk through till as long as there's an element |
6 | if (el.nodeName.toUpperCase() == "IMG") { // found an image |
7 | // flickr uses "_s" suffix for small, and "_m" for big images respectively |
8 | return el.src.replace(/_s\.jpg$/, "_m.jpg"); |
9 | } |
10 | el = el.nextSibling; |
11 | } |
12 | |
13 | return ""; |
view plain | print | ? |
Finally render both the controls.
1 | carousel.render(); |
2 | carousel.show(); |
3 | paginator.render(); |
view plain | print | ? |
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.
Note: Logging and debugging is currently turned off for this example.
Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings