Carousel Widget: Spotlight Example
This example uses the YUI Carousel Widget to showcase a simple spotlight example using its "itemSelected" event. In this example, you can use arrow keys to select items, as well as click on an item to select it.
Though this functionality looks a little complicated, it is very easy to implement. This is because the YUI Carousel Widget handles the keyboard event and the mouse click event for setting the selection. When an item is selected, the YUI Carousel Widget triggers a "itemSelected" event. This example subscribes to the "itemSelected" event to display the selected image.
Using the Carousel Widget for spotlight example
Here we will use the YUI Carousel Widget's "itemSelected" event to display the selected image.
This example has the following dependencies:
This example uses progressive enhancement. So, the Carousel is created from an "ordered" list of elements.
We will add a container where we would display the spotlight image.
We'll have only one CSS rule to set the height for the Carousel items.
Since we have the elements in place, we can invoke the Carousel's constructor to create the widget. The Carousel's "selectedItem" property returns the index of the currently selected item. So, using that property and the getElementForItem() API we can display the image of the selected item when the Carousel is rendered.
Now, we can subscribe to the "selectedItem" 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 (LI in our case).
Once the reference to the Carousel's item is obtained, it is straight-forward to implement a function that extracts the image within it.
Lets assemble everything together. The full JavaScript reads as follows: