SimpleCarousel
On CNET, we have a rather common layout that you've no doubt seen elsewhere on the web. We call it a carousel but who knows what others call it. At it's heart, it's some sort of box with content and buttons that cycle through it.
Here are some examples of carousels on our network (these are just images):
So in it's most basic form, a carousel is a bunch of elements (slides) and related interactive elements (buttons). It is animated (i.e. it "plays" through the items) and the user can optionally stop the animation either by clicking a button or mousing over it.
Enter the SimpleCarousel class, which handles all that interactivity for you - again, the html and what it looks like is up to you entirely.
Here's a very, very simple carousel:
The html we'll use:
<style> #simpleCarousel td div { position: absolute; } #simpleCarousel td.button { padding: 5px; } #simpleCarousel td.selected { font-weight: bold; } #simpleCarousel td.slides { height: 20px; } </style> <table id="simpleCarousel"> <tr><td colspan="4" class="slides"> <div class="slide">I'm the first content slide</div> <div class="slide">I'm the second content slide</div> <div class="slide">I'm the third content slide</div> <div class="slide">I'm the fourth content slide</div> </td></tr> <tr> <td class="button">one</td> <td class="button">two</td> <td class="button">three</td> <td class="button">four</td> </tr> </table>
...here's that html rendered:
| one | two | three | four |
new SimpleCarousel($('simpleCarousel'), $$('#simpleCarousel div.slide'), $$('#simpleCarousel td.button'), { slideInterval: 1000, rotateAction: 'click' }); //the default slideInterval is 4 seconds, //let's make this example go a little faster

In it's default state, the carousel just makes the slides animate. Note that it doesn't position these elements. You'll need your CSS to do that for you. Each slide should be positioned absolutely (on top of each other).
The buttons don't do anything related to the animation in this example. As each slide cycles, it adds a class name to the button ("selected" when the corresponding slide is visible, "off" when it isn't).
Your buttons will probably be links to the content on some other page, and you'll probably make the slide content also clickable. The SimpleCarousel class has several options, so be sure to check the docs.
A more complex example
This doesn't look so hot until you execute the carousel code:
new SimpleCarousel($('musicCarousel'), $$('#musicCarousel div.slide'), $$('#musicCarousel a.button'), { rotateAction: 'click', /*lets make the user click to jump*/ slideInterval: 2000, onShowSlide: function(){ /*this layout uses images that change, not classes so we have to add a little more logic here*/ $$('#musicCarousel div.panel-top a.off img').each(function(button, index){ button.src = button.src.replace('_on', '_off'); }); $$('#musicCarousel div.panel-top a.selected img').each(function(button, index){ button.src = button.src.replace('_off','_on'); }); } });

You can click the numbers at the top to manually see a slide. This also stops the animation.
.hide, .show, .stop and more
You can call .hide() on your instance to hide the component from view (sets visibility to hidden; does not alter display property), and .show() to show the component. This is useful if you want to make sure it looks good before it's visible to the user.
.stop will stop the animation (autoplay). .rotate will iterate to the next slide. .autoplay will start the animation.
cnet-libraries/08-layout/03-simplecarousel.txt · Last modified: 2008/11/17 23:25 by aaron-n












