Reputation: 159
I have the following slider:
https://jsfiddle.net/lucasmosele/px9ar93y/1/
My interval code is as follows:
var counter = 1;
var elements = json.slider.length;
// Set timer between quotes being visible
$("#quotes li:first-child").addClass("active");
int = setInterval(function(){
$("#quotes li:nth-child(" + counter + ")").removeClass("active");
$("#quotes li:nth-child(" + counter + ")").next().addClass("active");
if (counter === elements){
counter = 1;
} else {
counter++;
}
}, 3000);
I want to be able to have the new content fadeIn, I have tried .fadeIn:
$("#quotes li:nth-child(" + counter + ")").next().addClass("active").fadeIn("slow");
but I got behavior that I didn't necessarily want. I also tried creating css transitions between li and li.active, but for some reason they dont show up unless .fadeIn is set to "slow".
Is there a cleaner way to do this?
Upvotes: 0
Views: 90
Reputation: 11275
I think this is what you looking for:
$("#quotes li:first-child").addClass("active");
int = setInterval(function(){
$("#quotes li:nth-child(" + counter + ")").fadeOut('slow', function() {
$("#quotes li:nth-child(" + counter + ")").next().fadeIn('slow');
});
if (counter === elements){
counter = 1;
} else {
counter++;
}
}, 3000);
Here is the jsfiddle: https://jsfiddle.net/px9ar93y/6/
Upvotes: 0
Reputation: 36703
You can do
$("#quotes li:nth-child(" + counter + ")").next().hide().addClass("active").fadeIn("slow");
Upvotes: 1