Here's my code & Demo : http://jsbin.com/ixoqu3
The Basic rule is restrict the animation when the
1) (current Image index) * (Image width) of the slider is greater than total width
2) (current Image index) * (Image width) of the slider is less than 0 (zero)
The following JavaScript Code tells the same, Check out the Working Demo
HTML :
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<meta charset=utf-8 />
<title>Hello world !!</title>
<style type="text/css">
body{
background:#000;
color:#fff;
}
#slider {
padding:0px;
margin:0px auto;
overflow:hidden;
width:500px;
border:10px solid #d5d5d5;
-moz-border-radius:10px;
-webkit-border-radius:10px;
border-radius:10px;
-moz-box-shadow:0px 0px 20px #f0f;
-webkit-box-shadow:0px 0px 20px #f0f;
box-shadow:0px 0px 20px #f0f;
}
ul {
padding:0px;
margin:0px;
position:relative;
list-style:none;
}
li {
float:left;
padding:0px;
margin:0px;
}
#nav {
margin:0px auto;
width:200px;
}
span{
padding:10px;
background:#3f3f3f;
color:#fff;
font:bold 16px verdana;
-moz-border-radius:10px;
border:1px solid #fff;
-moz-box-shadow:0px 0px 30px #0099f9;
-webkit-box-shadow:0px 0px 30px #0099f9;
box-shadow:0px 0px 30px #0099f9;
cursor:pointer;
}
#num {
padding:5px;
background:#000;
}
</style>
</head>
<body>
<div id="slider">
<ul>
<li>
<img src="http://www.autoblog.com/media/2006/05/cars-movie.png"/>
</li>
<li>
<img src="http://www.mediabistro.com/fishbowlLA/original/carpass.jpeg.jpg"/>
</li>
<li>
<img src="http://disney-clipart.com/Cars/Disney-Cars-McQueen.jpg"/>
</li>
<li>
<img src="http://www.cargurus.com/blog/wp-content/uploads/2009/01/cars-lightning-mcqueen.jpg"/>
</li>
<li>
<img src="http://www.grahammilton.com/folio/folio_cars1.jpg"/>
</li>
</ul>
</div>
<br /><br />
<div id="nav"
<span id="prev"> Prev </span>
<span id="next"> Next </span>
<br /><br /><br />
Slide number : <span id="num">1</span>
</div>
</body>
</html>
JavaScript :
var img_width = 500;
var img_height = 250;
var slide = 0;
var speed = 500;
var size = 0;
$(document).ready(function() {
size = $('#slider').find('img').length;
$('#slider').find('ul').width(img_width * size).height(img_height);
$('#slider li, #slider img').width(img_width).height(img_height);
$('#next').bind('click',function() {
if(slide > img_width * (size - 1) *(-1)) {
slide -= img_width;
slider(slide);
}
});
$('#prev').bind('click',function() {
if(slide < 0) {
slide += img_width;
slider(slide);
}
});
});
function slider(slideMargin) {
$('#slider ul').stop().animate({'left':slideMargin}, speed ,function(){
$('#num').text(Math.abs(slideMargin/ (100 *size)) + 1);
});
}