Slider Gallery
Posted on 18th February 2008 — Michiel Kenis requested a tutorial explaining how to create a similar effect used to showcase the products on the Apple web site.








Posted on 18th February 2008 — Michiel Kenis requested a tutorial explaining how to create a similar effect used to showcase the products on the Apple web site.
This ‘product slider’ is similar to a straight forward gallery, except that there is a slider to navigate the items, i.e. the bit the user controls to view the items. Simple stuff.
jQuery already has the plugins to create these effects so we don’t have to go about creating them ourselves from scratch.
How to Solve the Problem
To create the slider controlled gallery, we need the following:
I’ve provided a screencast to walk through how create this functionality. Details on how and what I used can be found below.
Watch the slider gallery screencast (alternative flash version)
View the demo and source code used in the screencast
jQuery UI
jQuery UI is “a set of themable widgets and interactions”. For this task, we don’t need much of jQuery UI, but for the UI slider widget, we’ll need the following:
Of course we’ll still need the latest jquery.js.
All the above scripts can be extracted from the jQuery UI download.
HTML Markup
The markup for the example is straight forward. It boils down to a
ulholding the items you want in the gallery, and adivwith another nesteddiv, for the slider and it’s handle.CSS
One key feature about the CSS used: in my example, I assumed that I would not have a predefined width to the
ulelement. As such, I can’t float thelielements and have set widths. To ensure everything flows horizontally, I make use ofwhite-space: nowrap.I’ve only included the required CSS to create the sliding gallery effect. If you want to see the complete CSS used in the screencast just view the source for the demo
Slider Gallery Container
The overflow ensures the items are hidden, and the
position: relativeis used to absolutely position theulelement within it.Slider Gallery Items
The
white-space: nowrapis what allows us to work with an unknown width in theulelement.Slider Handle
In the screencast, the example uses a background image for the slider handle, and in particular from the example, the width of the handle is larger than the image to allow it to overlay on the left and right arrows.
jQuery
As stated above, we will need to include a series of plugins from the jQuery UI download, then we will add our dash of jQuery to create the sliding gallery effect.
Required Plugins
Slider Code
Please note during the screencast I used
$(function () {})– but I’ve had to since change this to$(window).ready. This is because I’m usingwhite-space: nowrapand the width isn’t calculated until after the images are loaded. jQuery’s document ready function fires before the images are loaded – to allow the JavaScript access to the DOM as early as possible.This wasn’t a problem in the demo because the images were loaded from a local server, and thus loaded fast enough to allow the code to work out the width correctly.
The code breaks down as follows:
When the DOM and images are ready (note that if you don’t use
white-space: nowrapthen you should use the standard$(document).ready()method).Loop through all the
div.sliderGalleryelements. This code design sets us up to convert our code to a reusable plugin.Search for the
ulin the context of this – which is currently adiv.sliderGallery..innerWidth()and.outerWidth()come from jquery.dimensions.js. We’re subtracting the currentdiv.sliderGallerywidth because when we slide, we want it to stop once the last item is visible, rather than sliding it all the way left until it’s out of sight..slider()comes from ui.slider.js.The class of our slider handle.
As the handle is moved, we move the ul of items, note that it moves negatively, creating the scrolling effect.
We use this for when slider area has been clicked, to create the scroll effect.
Taking it Further
When I have a working version of the code, I often try to think how the interaction could be better. Here’s a list of ways it could be improved, in no particular order – if you fancy a challenge:
You should follow me on Twitter here I tweet about jQuery amongst the usual tweet-splurges!