Tasneem Fathima
Tasneem Fathima

Reputation: 159

sliding divs horizontally using jquery

I am trying to design a slider for my page and I am wondering if anyone knows how to create sliding divs that come from the left to right, and vice versa only when the corresponding button(left & right) is pressed? Can you help?

Here is the code I am trying. I can get the slides to work, but on reaching the ends it still keep sliding right or left continuosly. i want it to stop dynamically once it reaches the ends and on clicking further should alert thru pop up.

can anyone guide me with some example coding

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Sample Slide</title>
<style type="text/css">
.total
{
height:350px;
width:75%;
border:1px solid black;
margin-left:auto;
margin-right:auto;
margin-top:15%;
}
.slidepanel
{
border:1px solid purple;
width:100%;
height:100%;
overflow-x: scroll;
overflow-y: hidden;
}
.box-wrapper 
{
width: 400%;
height: 90%;
overflow: hidden;
}

.block
{
 /* position:absolute;
  background-color:#abc;
  left:50px;
  width:90px;
  height:90px;
  margin:5px;*/
border:1px solid red;
width:24.9%;
height:98%;
float:left;
margin-left:auto;
margin-right:auto;
position:relative;
left:0px;
}
.block:nth-child(odd) {
    background: cyan;
}
.block:nth-child(even) {
    background: red;
}
</style>
 <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
 <script type="text/javascript" src="jquery.js"> </script>
<script type="text/javascript">
 $(document).ready(function() {
$("#sright").click(function(){
  $("#block1,#block2,#block3 ,#block4").animate({"left": "+=24.9%"}, "slow");
   });
});

 $(document).ready(function() {
$("#sleft").click(function(){
  $("#block1,#block2,#block3 ,#block4").animate({"left": "-=24.9%"}, "slow");
});
});

</script>
</head>
<body>
<div class="total">
<div class="slidepanel">
<center><button id="sleft">&laquo;</button> <button id="sright">&raquo;</button></center>
<div class="box-wrapper">
<div class="block" id ="block1"></div>
<div class="block" id ="block2"></div>
<div class="block" id ="block3"></div>
<div class="block" id ="block4"></div>
</div>
</div>
</div>
</body>
</html>

Upvotes: 2

Views: 7487

Answers (2)

sree
sree

Reputation: 498

Try animating in px like this...

Make sure ur all div are available at the time of animation , then try this for one block div

$('#block1').animate({"left": "-=150px"}, 500);

count the no of images available.

on click make sure that max image is not exceeded and u revert back to the first image again, animating back to first image to 0px.

similarly , check for the first image and then when total image width is lesser then total slider width , then do not allow to animate the images to right side.

see if this helps slider tut

Upvotes: 0

Tats_innit
Tats_innit

Reputation: 34107

[Working Demo][1] : http://jsfiddle.net/XJVYj/ (using your code above)

Hope this helps, This is different from my previous reply of something on the same line: Stop .animate() when it reaches last div

anyhow this demo and code will give you exactly what you looking for.

Rest I will leave the code do the talking;

please note: I have tried to make it simple by using class attribute instead of chained id of your div element.

Jquery Code

$(document).ready(function() {

var cur = 1;
var max = $(".box-wrapper div").length;


$("#sright").click(function(){
    if (cur == 1 && cur < max)
        return false;
       cur--;

     $(".block").animate({"left": "+=24.9%"}, "slow");

});

$("#sleft").click(function(){
  if (cur+1 > max) 
      return false;
    cur++; 

   $(".block").animate({"left": "-=24.9%"}, "slow");
});
});​

Upvotes: 1

Related Questions