Sei sulla pagina 1di 4

Slider Gallery

http://www.rafaeldesigner.com.br/blog/?Uk1db6fU Demonstrao: http://www.rafaeldesigner.com.br/blog/?Uk1db6fU

jQuery UI
jQuery UI is a set of themable widgets and interactions. For this task, we dont need much of jQuery UI, but for the UI slider widget, well need the following:

jquery.dimensions.js ui.mouse.js ui.slider.js

Of course well 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 ul holding the items you want in the gallery, and a div with another nested div, for the slider and its handle.
<div class="sliderGallery"> <ul class="items"> <li>Item one</li> <li>Item two</li> <li>Item three, etc...</li> </ul> <div class="slider"> <!-- the handler to action the slide --> <div class="handle"></div> <!-- labels appear against the slider, as pointers to the user --> <span class="slider-lb1">slider label 1</span> <span class="slider-lb2">slider label 2</span> <span class="slider-lb3">slider label 3</span> </div> </div>

CSS
One key feature about the CSS used: in my example, I assumed that I would not have a predefined width to the ul element. As such, I cant float the li elements and have set widths. To ensure everything flows horizontally, I make use of white-space: nowrap. Ive 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: relative is used to absolutely position the ul element within it.
.sliderGallery { overflow: hidden; position: relative; padding: 10px; height: 160px; width: 960px; }

Slider Gallery Items


The white-space: nowrap is what allows us to work with an unknown width in the ul element.
.sliderGallery UL { position: absolute; list-style: none; overflow: none; white-space: nowrap; padding: 0; margin: 0; } .sliderGallery UL LI { display: inline; }

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.
.handle { position: absolute; cursor: move; top: 0; z-index: 100; /* bespoke to your own solution */ height: 17px; width: 181px; }

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
<script src="jquery.js" type="text/javascript"></script> <script src="jquery.dimensions.js" type="text/javascript"></script> <script src="ui.mouse.js" type="text/javascript"></script> <script src="ui.slider.js" type="text/javascript"></script>

Slider Code
// see note below $(window).ready(function () { $('div.sliderGallery').each(function () { var ul = $('ul', this); var productWidth = ul.innerWidth() - $(this).outerWidth(); var slider = $('.slider', this).slider({ handle: '.handle', minValue: 0, maxValue: productWidth, slide: function (ev, ui) { ul.css('left', '-' + ui.value + 'px'); }, stop: function (ev, ui) { ul.animate({ 'left' : '-' + ui.value + 'px' }, 500, 'linear'); } }); }); });

Please note during the screencast I used $(function () {}) - but Ive had to since change this to $ (window).ready. This is because Im using white-space: nowrap and the width isnt calculated until after the images are loaded. jQuerys document ready function fires before the images are loaded - to allow the JavaScript access to the DOM as early as possible.
This wasnt 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:


window.onload = function () {

When the DOM and images are ready (note that if you dont use white-space: nowrap then you should use the standard $(document).ready() method).
$('div.sliderGallery').each(function () {

Loop through all the div.sliderGallery elements. This code design sets us up to convert our code to a reusable plugin.
var ul = $('ul', this);

Search for the ul in the context of this - which is currently a div.sliderGallery.


var productWidth = ul.innerWidth() - $(this).outerWidth(); .innerWidth() and .outerWidth() come from jquery.dimensions.js. Were subtracting the current div.sliderGallery width because when we slide, we want it to stop once the last item is visible, rather

than sliding it all the way left until its out of sight.
var slider = $('.slider', this).slider({ .slider()

comes from ui.slider.js.

handle: '.handle',

The class of our slider handle.


slide: function (ev, ui) { ul.css('left', '-' + ui.value + 'px'); },

As the handle is moved, we move the ul of items, note that it moves negatively, creating the scrolling effect.
stop: function (ev, ui) { ul.animate({ 'left' : '-' + ui.value + 'px' }, 500, 'linear'); }

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. Heres a list of ways it could be improved, in no particular order - if you fancy a challenge: 1. Better scrolling effect, in particular, fast scroll for the bulk of the animation, and slows down when its settling on the final item. 2. Turn it in to a plugin. 3. The arrow can be turned in to handlers to slide the handle only part of the distance, rather than all. 4. A nice effect the Apple version has, is the jump labels, within the slider element, change class given the distance they are from the slider handle. They appear brighter as theyre closer and duller as they are further away from the handle. 5. Anything else you can think of!

Potrebbero piacerti anche