Developer Network Home - Help

Yahoo! UI Library: Carousel Control

Yahoo! UI Library: Carousel Control

The YUI Carousel Control provides a widget for browsing among a set of like objects arrayed vertically or horizontally in an overloaded page region. The "carousel" metaphor derives from an analogy to slide carousels in the days of film photography; the Carousel Control can maintain fidelity to this metaphor by allowing a continuous, circular navigation through all of the content blocks.

The Carousel Control can consume content from page markup (progressive enhancement); alternatively, the Carousel can be created, configured and populated entirely via script. Support for lazy-loading of content via XMLHttpRequest (using Connection Manager) is built in.

You can read more about the Carousel Design Pattern at the Yahoo! Design Pattern Library.

Note: The Carousel Control is being released as a beta component. Please refer to the FAQ for what we mean by this designation. We look forward to your feedback in the YUI Forums.

Getting Started

To use the Carousel Control, include the following source files in your web page:

YUI dependency configurator.

YUI Dependency Configurator:

Instead of copying and pasting the filepaths above, try letting the YUI dependency Configurator determine the optimal file list for your desired components; the Configurator uses YUI Loader to write out the full HTML for including the precise files you need for your implementation.

Note: If you wish to include this component via the YUI Loader, its module name is carousel. (Click here for the full list of module names for YUI Loader.)

Where these files come from: The files included using the text above will be served from Yahoo! servers; see "Serving YUI Files from Yahoo!" for important information about this service. JavaScript files are minified, meaning that comments and white space have been removed to make them more efficient to download. To use the full, commented versions or the -debug versions of YUI JavaScript files, please download the library distribution and host the files on your own server.

Order matters: As is the case generally with JavaScript and CSS, order matters; these files should be included in the order specified above. If you include files in the wrong order, errors may result.

Basic Setup

The Carousel Control can be created with or without existing HTML. In either case, the Carousel's HTML is composed of a container <div> and a list of items within the container <ol> or <ul>. The Carousel Control can construct the navigation elements also, if these are not already provided by the markup.

The Carousel Control is defined by YAHOO.widget.Carousel and can be instantiated two ways:

Using an existing list <ol> or <ul> element

A Carousel Control can be created from an existing list of items <ol> or <ul>.

Pass the id of the Carousel's container element as the first argument to the Carousel's constructor. If any additional configuration is needed, it can be set during the instantiation by specifying them in the object literal that is passed as the second argument to the Carousel's constructor. Finally, call the render method of the Carousel to display the widget.

Using no existing HTML

A Carousel Control can be created purely from JavaScript, without any existing HTML.

While calling the render method when the Carousel is constructed from JavaScript, the id attribute of the parent container that would contain the widget needs to be passed to it.

Using Carousel

This section describes several common uses and customizations of the Carousel Control:

Using the Carousel with YAHOO.util.Event.onContentReady

As the Carousel Control can be used to progressively enhance a list of items, it is recommended that the Carousel Control be instantiated as soon as the contents of the Carousel Widget's container are ready. This helps minimize any flicker or redraw that might occur when the DOM structure is modified during widget instantiation.

To instantiate a Carousel instance as soon as the content for the Carousel's container element is available, use the onContentReady method of the YUI Event Utility. The example below illustrates how to do this. Consider the following markup in your page:

To instantiate the Carousel Control as soon as the content is available, pass the id of the container as the first argument to YAHOO.util.Event.onContentReady method. The second parameter to the method is the function in which you instantiate the widget.

Usage of the onContentReady method is demonstrated in many of the Carousel Widget examples. For more information, read the section titled Using the onAvailable and onContentReady Methods section of the Event utility's landing page.

Animated scrolling of the Carousel contents

If you include the YUI Animation Utility in your web page, you can enable animation on item scroll in a Carousel using the following syntax:

Alternatively, you can set this property during the instantiation of Carousel itself.

Setting up auto-play in Carousel

By default, the Carousel scrolls its contents only when the previous or next arrows are clicked, or when the pagination control is clicked. You can make the Carousel automatically scroll after a certain delay by setting its autoPlay configuration setting. The following example illustrates setting this property and also making the Carousel circular.

Alternatively, you can set this property during the instantiation of Carousel itself.

Revealing a "sneak preview" of the previous and next items in a Carousel

There are times when you want to provide a slight reveal for the items before and after the clipping region in the Carousel Control. This gives the user a sneak preview of the forthcoming items and invites the user to explore the content.

You can set the revealAmount to the percentage of the size of an item to be revealed. The following example illustrates setting the property.

Alternatively, you can set this property during the instantiation of Carousel itself.

A vertical Carousel

If you want to display a vertical Carousel Control, you can set the isVertical property to true. The following example illustrates creating a vertical Carousel Widget.

Alternatively, you can set this property during the instantiation of Carousel itself.

Handling Events

All events expressed as part of the to subscribe to all of the Carousel Control's API can be subscribed using the on (or addListener) method (as is true of all YUI controls based on the Element Utility). This includes even the DOM-based events like click.

For custom events specific to this component, a data object is passed to the handler function as the first argument. For DOM events, this is the actual event object.

For the Carousel Control-specific events, the first argument passed to the event listener is the information about the event. This varies based on the event that is triggered. For example, for the scroll events the data object tells you about the first and last visible items in the Carousel and the direction of scroll.

Note: If you want to abort the scrolling of the Carousel, you can subscribe to the beforeScroll event and then return the value false to abort scrolling.

The event listeners can be removed via the removeListener method of the Carousel Control.

Configuration Attributes

The Carousel Control's configuration can be obtained or set at runtime using its get and set methods. For example:

See the reference table below as well as the API documentation for Carousel Control for the complete list of configuration attributes.

Skinning the Carousel Control

Carousel comes with a default presentation or "skin," part of the "Sam Skin" visual treatment that accompanies most YUI controls. You can read more about the general approach to skinning YUI components in this in-depth article.

The CSS provided with Carousel is comprised of core, functional CSS as well as the Sam Skin visual treatment.

Carousel Widget using the Sam Skin

Reference Tables

The following tables contain information about configuration properties and CSS classes for the Carousel Widget:

Name Type Default Description
firstVisible Number 0 The index to start the Carousel from (indices begin from zero)
numVisible Number 3 The number of visible items in the Carousel's viewport
scrollIncrement Number 1 The number of items to scroll by for arrow keys.
selectedItem Number 0 The index of the selected item in the Carousel
revealAmount Number 0 The percentage of the item to be revealed on each side of the Carousel (before and after the first and last item in the Carousel's viewport.)
isCircular Boolean false Boolean to enable continuous scrolling of elements in the Carousel.
isVertical Boolean false Boolean to enable vertical orientation of the Carousel Widget.
navigation Object { prev: null, next: null } The set of navigation controls for the Carousel. The Sam skin provides the set of navigation controls by default. If you need to override them with your own set of buttons, you can set the ID of the buttons using the navigation configuration setting.
animation Object { speed: 0, effect: null } The configuration of the animation attributes for the Carousel. The speed attribute takes the value in seconds; the effect attribute is used to set one of the Animation Utility's built-in effects (like YAHOO.util.Easing.easeOut)
autoPlay Number 0 Set this time in milli-seconds to have the Carousel scroll the elements automatically.
numItems Number Set the number of items in the Carousel. If the number of items specified is less than the number of items in the Carousel, the excess elements are removed from the Carousel. If the number of items specified is greater, then the loadItemsHandler is called to load the additional items into the widget.
loadItemsHandler Function Set the lazy-load items handler for dynamically loading items in the Carousel.
See the API documentation for the complete list of properties and methods.

The Carousel Control makes use of several CSS classes. You can either use these CSS classes that come as part of the Sam skinning or provide a different style sheet to override their values. The following table lists the CSS classes used by the Carousel Control:

Name Description
yui-carousel This class is applied to the Carousel's container element.
yui-carousel-horizontal This class is applied to the Carousel's container element when its orientation is set to horizontal.
yui-carousel-vertical This class is applied to the Carousel's container element when its orientation is set to vertical.
yui-carousel-visible This class is applied to the Carousel's container when it is visible.
yui-carousel-content This class is applied to the clipping region within the Carousel Widget.
yui-nav This class is applied to the navigation container.
yui-carousel-button This class is applied to the Carousel's navigation buttons.
yui-carousel-button-disabled This class is applied to the Carousel's navigation buttons when they are disabled.
yui-carousel-pages This class is applied to the Carousel's page navigation buttons.

Known Issues

YUI on Mobile: Using Carousel Control with "A-Grade" Mobile Browsers

About this Section: YUI generally works well with mobile browsers that are based on A-Grade browser foundations. For example, Nokia's N-series phones, including the N95, use a browser based on Webkit — the same foundation shared by Apple's Safari browser, which is found on the iPhone. The fundamental challenges in developing for this emerging class of full, A-Grade-derived browsers on handheld devices are:

  • Screen size: You have a much smaller canvas;
  • Input devices: Mobile devices generally do not have mouse input, and therefore are missing some or all mouse events (like mouseover);
  • Processor power: Mobile devices have slower processors that can more easily be saturated by JavaScript and DOM interactions — and processor usage affects things like battery life in ways that don't have analogues in desktop browsers;
  • Latency: Most mobile devices have a much higher latency on the network than do terrestrially networked PCs; this can make pages with many script, css or other types of external files load much more slowly.

There are other considerations, many of them device/browser specific (for example, current versions of the iPhone's Safari browser do not support Flash). The goal of these sections on YUI User's Guides is to provide you some preliminary insights about how specific components perform on this emerging class of mobile devices. Although we have not done exhaustive testing, and although these browsers are revving quickly and present a moving target, our goal is to provide some early, provisional advice to help you get started as you contemplate how your YUI-based application will render in the mobile world.

More Information:

Support & Community

The YUI Library and related topics are discussed on the on the ydn-javascript mailing list.

In addition, please visit the YUIBlog for updates and articles about the YUI Library written by the library's developers.

Filing Bugs & Feature Requests

The YUI Library's public bug tracking and feature request repositories are located on the YUILibrary.com site. Before filing new feature requests or bug reports, please review our reporting guidelines.

Copyright © 2009 Yahoo! Inc. All rights reserved.

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