updated on 30th June 2011
When I first answered this question, I was relatively new to jQuery. However, upon receiving an upvote for my answer recently, I revisited it and noticed room for improvement. I realized that the entry of the next element happened too quickly, disrupting the block layout (view here). Therefore, I believe utilizing a callback function after the initial toggle is the proper way to address this issue.
Updated jQuery Code
$('li:gt(4)').css('display', 'none');
$("button").click(function() {
$('li:first').insertAfter('li:last').toggle('clip', 100, function() {
// called after the first .toggle() is finished
$('li:eq(4)').toggle('scale', 100);
});
});
View the updated live example here.
.toggle( [duration,] [easing,] [callback] )
Duration: A string or number indicating how long the animation will run.
Easing: A string specifying which easing function to use for the transition.
Callback: A function to execute once the animation is complete.
Original Approach:
JQUERY
$('li:gt(4)').css('display', 'none');
$("button").click(function() {
$('li:first').fadeOut('fast').insertAfter('li:last')
$('li:eq(4)').fadeIn(); });
HTML
<ul>
<li class="slider"> Item-1 </li>
<li class="slider"> Item-2 </li>
<li class="slider"> Item-3 </li>
<li class="slider"> Item-4 </li>
<li class="slider"> Item-5 </li>
<li class="slider"> Item-6 </li>
<li class="slider"> Item-7 </li>
<li class="slider"> Item-8 </li>
<li class="slider"> Item-9 </li>
<li class="slider"> Item-10 </li>
</ul>
<button> Next > </button>
For a clearer and more concise solution, consider using the updated code provided above. It offers improved readability and flexibility.
The updated functionality selects any list item greater than index 4 (zero-based) and hides it. When the button is clicked, the first item eases out and moves to the end, while the fourth item eases in. The animations can be customized further by utilizing jQuery UI effects with toggle. This revised method proves to be efficient and easy to understand.
EDIT:
Upon further enhancements, I decided to create a separate question and share the improved answer along with a fiddle demonstration.
$('li:gt(4)').css('display', 'none');
$("button").click(function() {
$('li:first').insertAfter('li:last').toggle('clip',100);
$('li:eq(4)').toggle('scale', 100);
});
Explore the updated fiddle link here: http://jsfiddle.net/67Wu7/