I made a vertical jQuery slideshow to where if you click 3 different images the table slides. This works perfect, but I also need it to slide on a timed function. I have tried doing loops with timeout functions but then my code for the click doesn’t work and the timing feature doesn’t even work. The attempt at a timing is at the bottom if it could just have a small mistake or something.
Original click function (works)
$(document).ready(function() {
$("#slide1-1").click(function() {
$("#slide1_images").css("top","0");
$(".imagetab").css("opacity","1.0");
$(".imagetab").css("filter",'alpha(opacity="100")');
$(".imagetab2").css("opacity",".4");
$(".imagetab2").css("filter",'alpha(opacity="40")');
$(".imagetab3").css("opacity",".4");
$(".imagetab3").css("filter",'alpha(opacity="40")');
});
$("#slide1-2").click(function() {
$("#slide1_images").css("top","-430px");
$(".imagetab2").css("opacity","1.0");
$(".imagetab2").css("filter",'alpha(opacity="100")');
$(".imagetab").css("opacity",".4");
$(".imagetab").css("filter",'alpha(opacity="40")');
$(".imagetab3").css("opacity",".4");
$(".imagetab3").css("filter",'alpha(opacity="40")');
});
$("#slide1-3").click(function() {
$("#slide1_images").css("top","-860px");
$(".imagetab3").css("opacity","1.0");
$(".imagetab3").css("filter",'alpha(opacity="100")');
$(".imagetab2").css("opacity",".4");
$(".imagetab2").css("filter",'alpha(opacity="40")');
$(".imagetab").css("opacity",".4");
$(".imagetab").css("filter",'alpha(opacity="40")');
});
});
Timing attempt (doesn’t work)
$(document).ready(function() {
var slide = function() {
$("#slide1_images").css("top","0");
}
var slide2 = function() {
$("#slide1_images").css("top","-430px");
}
var slide3 = function() {
$("#slide1_images").css("top","-860px");
}
var i;
for(i = 0; i < element.length; ++i) {
if (i=="1")
{
setTimeout(slide, 2000);
}
if (i=="2")
{
setTimeout(slide2, 2000);
}
if (i=="3")
{
setTimeout(slide3, 2000);
}
if (i=="4")
{
var i = "0";
}
}
*INSERT CODE FROM ABOVE HERE.*
});
You should be able to generalize your solution instead of duplicating so much of the code, since you do more or less the same thing for each tab. This solution reduces a lot of the redundancy:
Also, it should be noted that you don’t even need
setTimeoutfor these animations; jQuery has a built in.animate()function for modifying css properties with a smoother transition: