views:

422

answers:

2

I needed a simple three slides slider, so instead of using a jQ plugin I hand coded it myself. The code works but the animation occurs in 2-3 frames instead of happening in over 20 frames, except when in IE6 & IE7 where it animates perfectly in 20-30 frames. In all the other browsers(IE8, Firefox, Chrome, Safari, Opera) it animates in a motion which seems like the computer is hanging. If anyone knows why this is happening please lemme know. I don't wanna use a bulky plugin and then style it too.

HTML:

<div class="fl-left" id="slide-box">
<div class="slide" id="slide-1">
 <img src="images/slider/1.jpg" alt="The Image Description" />
 <p class="slide-text">&quot;The Description About The Image/Slide&quot;</p>
</div>

<div class="slide" id="slide-2">
 <img src="images/slider/1.jpg" alt="The Image Description" />
 <p class="slide-text">&quot;The Description About The Image/Slide&quot;</p>
</div>

<div class="slide" id="slide-3">
 <img src="images/slider/1.jpg" alt="The Image Description" />
 <p class="slide-text">&quot;The Description About The Image/Slide&quot;</p>
</div>
</div>

CSS:

#slide-box{
position:relative;
width:472px;
height:192px;
border-bottom:3px solid #fff;
overflow:hidden;
}

.slide{ position:absolute;
float:left; width:455px;
overflow:hidden;
border:1px solid #000;
margin:8px 8px 0 8px;
}

.slide img{ float:left; }
.slide-text{ display:block;
width: 140px;
height:132px;
background:#ecefdc;
float:left;
padding: 10px 0px;
}

#slide-1 { left:0px; }
#slide-2 { left:467px; }
#slide-3 { left:934px; }

jQuery:

$('#slide-but-1').click(function(){
$('#slide-1').animate({"left": "0px"}, "slow");
$('#slide-2').animate({"left": "467px"}, "slow");
$('#slide-3').animate({"left": "934px"}, "slow");
});
$('#slide-but-2').click(function(){
$('#slide-1').animate({"left": "-467px"}, "slow");
$('#slide-2').animate({"left": "0px"}, "slow");
$('#slide-3').animate({"left": "467px"}, "slow");
});
$('#slide-but-3').click(function(){
$('#slide-1').animate({"left": "-934px"}, "slow");
$('#slide-2').animate({"left": "-467px"}, "slow");
$('#slide-3').animate({"left": "0px"}, "slow");
});
+3  A: 

I suspect that the problem lies in the styling of the slides, namely the float: left - it's redundant, since you use absolute positioning.

Furthermore, you could achieve better performance if you have an additional container of the slides, like this:

<div class="fl-left" id="slide-box">
    <div id="slide-container">
         <div class="slide" />
         <div class="slide" />
         <div class="slide" />
    </div>
</div>

This way you could animate only the container, $('#slide-container').animate({ left: 0 }, "slow"); - and the resulting code will be cleaner.

Alexander Gyoshev
I did the container thing initially coz obviously it reduces my jQuery code but it was hanging therefore I thought that I should try to animate each slide separately, but that didn't really change anything.I know about the float:left but removing it spoils my layout for some reason.
Aayush
A: 

The code was clashing with the jQuery Drop Shadow plugin I was using. Removing that made the animation smooth.

Aayush