Simple Slider using JQuery Animation

Step 1 : Create a html file and add the following script tag in head section
             <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript">

Step 2 : add the following code to display the images
           
             <div id="wrapper">
<div id="slide">
<img src="7.JPG" width="200px" height="200px" />
<img src="8.JPG" width="200px" height="200px" />
<img src="9.JPG" width="200px" height="200px" />
<img src="11.JPG" width="200px" height="200px" />
<img src="12.JPG" width="200px" height="200px" />
</div>
   </div>

Step 3 : add the following code for next and previous button
            <div id="buttons">
<a href="javascript:void(0)" id="prev"><span style="text-decoration:none;"> < </span></a>
<a href="javascript:void(0)" id="next"><span style="text-decoration:none;"> > </span></a>
   </div>

Step 4: add the following css code
            *{
margin: 0 auto;
padding:0;
        }

#wrapper{
width:600px;
overflow:hidden;

}
#slide{
width:900px;
overflow:hidden;
height:210px;
position:relative;
}

#buttons{
position: relative;
width: 600px;
}


Step 5: add the following jquery to animate slide the images
            $(document).ready(function(){
               var timages = 5; <!-- timages = total number of images -->
var simages = 3; <1-- simages = number of images to show -->
var limages = timages-simages; <!-- limages = number of images remaining i.e. 5-3=2 -->
var cimages = 0; <!-- cimages = current images -->
$("#prev").click(function(){
cimages --;
if (cimages < 0)
{
cimages = 0
}
var posx = cimages* 200;
//showInputText();
$("#slide").animate({left:"-"+posx+"px"}, 500 )
});

$("#next").click(function(){
cimages ++;
if (cimages > limages)
{
cimages = limages
}
var posx = cimages* 200;
//showInputText();
$("#slide").animate({left:"-"+posx+"px"}, 500 )
})
 })



Your final code should look like this

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>

<title>Untitled Document</title>
<style>
*{
margin: 0 auto;
padding:0;
}

#wrapper{
width:600px;
overflow:hidden;

}
#slide{
width:900px;
overflow:hidden;
height:210px;
position:relative;
}

#buttons{
position: relative;
width: 600px;
}
</style>
<script>
$(document).ready(function(){
var timages = 5;
var simages = 3;
var limages = timages-simages;
var cimages = 0;
$("#prev").click(function(){
cimages --;
if (cimages < 0)
{
cimages = 0
}
var posx = cimages* 200;
//showInputText();
$("#slide").animate({left:"-"+posx+"px"}, 500 )
});

$("#next").click(function(){
cimages ++;
if (cimages > limages)
{
cimages = limages
}
var posx = cimages* 200;
//showInputText();
$("#slide").animate({left:"-"+posx+"px"}, 500 )
})
})

</script>
</head>

<body>
<div id="wrapper">
<div id="slide">
<img src="7.JPG" width="200px" height="200px" />
<img src="8.JPG" width="200px" height="200px" />
<img src="9.JPG" width="200px" height="200px" />
<img src="11.JPG" width="200px" height="200px" />
<img src="12.JPG" width="200px" height="200px" />
</div>
</div>
<div id="buttons">
<a href="javascript:void(0)" id="prev"><span style="text-decoration:none;"> < </span></a>
<a href="javascript:void(0)" id="next"><span style="text-decoration:none;"> > </span></a>
</div>
</body>
</html>


0 comments: