Feature slideshow using Javascript
Quite a few websites have features on their homepage rotating news stories and attractive imagery - it's a cool way to capture the user's attention. I'm thinking of items like on ITV.com (done using Flash) and LussoLuxury.com (done using an iFrame). They're attractive and quite practical, so let's look at making a really simple implementation of one.
I'm using Mootools release 1.2 for this (bundled in the download).
What it needs to do
All I want is a simple panel that shows an image and a strapline; this panel then fades out and a new one fades in every 8-10 seconds and continues forevermore. Nice and simple. So I've got 4 images (cars and boats) each with a strapline that I want to see rotating every 8 seconds. Simple, non?
XHTML
As always I've wrapped it in a container div, with a slideshow-container div wrapped around that (just to keep things tidy). Then each of the panels is set as a slideshow-item.
It's all very straight forward here; an image and the strapline (wrapped in the slideshow-text div. The important thing to note is the addition of the noscript class on the container. This is something I'm using to ensure that the page loads with different CSS rules when JS is disabled; because how I'm managing the transitions isn't very accessible otherwise - but we'll get to that in a minute.
CSS
I'm setting widths and height on the container which governs the panel's dimensions. I'm setting both width and height on the slideshow-item too with an overflow of hidden; this means that images larger than these dimensions will be cropped rather than breaking the layout.
What I'm doing is positioning each of the slideshow-item divs absolutely on top of each other, so they occupy the same space on the page. Therefore you'll only see one at any one time. I'm positioning the slideshow-text absolutely too at the bottom of the panel. Note the noscript class; this ensures that if the class of noscript is present, the slideshow-items are positioned relatively (one after another on the page, so all are visible). I'm using Javascript to remove this class on-dom-ready (see later). This means that users without Javascript will see all the panels on the page; it breaks layout but it is best for accessibility. You could probably make the container scroll on overflow to ensure layout doesn't break, but this is the method I'm sticking with.
Javascript
As always I'm initialising things onDomReady (to ensure the page has finished loading structure). I'm setting the obvious variables at the top:
