I’m trying to get the animation to slide 1st image out then 2nd image slide in, 2nd image slide out, 3rd image slide in and so on, as the code stands each image slides out fine but fades back in rather then sliding.
var s = 0,
t = 2000;
$(document).on('ready', slide);
function slide(){
var speed = setInterval(slider, t);
}
function slider(){
s++;
var sld = $('#slider img'),
imgs = sld.length;
if(s == imgs){
s = 0;
}
sld.animate({'marginLeft': '+=750px'}, t).fadeOut('fast').animate({'marginLeft': '-=750px'}, t).eq(s).fadeIn(t);
}
We should first change a bit the structure using an unordered list (
<ul>):Then we should use a
setTimeout()instead of a setInterval so to be sure that the next slide is launched once the animation is completed:This way we slide the image right, then the next one left and finally call the `slider()´ function again with a timeout.
To make this work we need to change the css code like this:
Here you can see a DEMO