Usually I would give you some links to existing slider scripts, but I donβt know a single one that resizes nicely.
So, I adapted the code from my own PHP exception handler :)
$('#list').each(function(){ var list = $(this), currentPos = 0, itemCount = list.children().length; $('.right, .left').click(function(){ // +100 = right, -100 = left var direction = $(this).hasClass('right') ? 100 : -100, nextIndex = direction > 0 ? currentPos + 1 : currentPos - 1; // do nothing if there is animation happening, // or if index is out of boundaries if($('> li', list).is(':animated') || (direction > 0 && nextIndex > itemCount - 1) || (direction < 0 && nextIndex < 0) ) return; var next = $('> li:eq(' + nextIndex + ')', list), current = $('> li:eq(' + currentPos + ')', list); currentPos = nextIndex; // determine if the link needs to be hidden $('.left, .right').removeClass('hide'); if(currentPos === 0 || currentPos === itemCount - 1) $(this).addClass('hide'); // adjust height of the container list.css('height', Math.max(current.outerHeight(true), next.outerHeight(true)) + 'px'); // make the current slide absolute current.css({ position: 'absolute', left: 0, top: 0, width: '100%', height: current.outerHeight(true) + 'px' }); // show the next slide next.css({ position: 'absolute', left: direction + '%', top: 0, width: '100%', height: next.outerHeight(true) + 'px' }).show().animate({left: '0%'}, 300, 'swing', function(){ next.css({ position: 'relative', left: 'auto', top: 'auto' }); }); // hide the current slide current.animate({left: (-direction) + '%'}, 300, 'swing', function(){ current.hide(); }); }); // mouse wheel action within the list area list.mousewheel(function(event, delta){ (delta > 0) ? $('.right').click() :$('.left').click(); }); });
CSS
.hide{ display:none; } #list{ position: relative; width: 100%; overflow: hidden; } #list > li{ display: none; } #list > li:first-child{ display: block; }
The HTML structure should look like this:
<a class="left"> left </a> <a class="right"> right </a> <ul id="list"> <li> ... </li> ... </ul>
Demo
mousewheel jQuery plugin. If you do not need mouse wheel support, delete the last event.