如何在jQuery中淡入/淡出计数器?

如何在jQuery中淡入/淡出计数器?,jquery,animation,loops,Jquery,Animation,Loops,所以我有一堆div,每个div都包含一个数字。我正试图设置一个计数器,它的计数如下: 1 2 3 2 3 4 3 4 5 4 5 1 1 2 3 等等。你可以看到我的当前代码 我试图使节点淡入淡出状态,并一次淡出一个节点,而不是显示/隐藏整个容器。以下是一种方法: //I didn't wrap the roller inside a function for this example, but you can pass //these values as arguments to a fun

所以我有一堆div,每个div都包含一个数字。我正试图设置一个计数器,它的计数如下:

1 2 3
2 3 4
3 4 5
4 5 1
1 2 3
等等。你可以看到我的当前代码

我试图使节点淡入淡出状态,并一次淡出一个节点,而不是显示/隐藏整个容器。

以下是一种方法:

//I didn't wrap the roller inside a function for this example, but you can pass
//these values as arguments to a function instead of assigning them here:
var $slides = $('#slides'),
    n = 3; //number of visible children (`.speaker`)

//init by hiding the children with index larger than `n`
$slides.children(':gt('+(n-1)+')').hide();

//note that :eq and :gt are 0-based, hence the n-1. You could also assign
//n -= 1 or n-- after receiving the n parameter if using a function wrapper
setInterval(function () {
    $slides.children(':first').fadeOut(600, function () {
        $slides.append(this).children(':eq('+(n-1)+')').fadeIn(600);
    });
}, 1500);

IMO对于这么简单的东西,代码有点太多了。添加效果应该是轻而易举的事。不使用固定高度容器的可重用代码:太累了,无法写出答案,祝你好运。非常感谢。你的方法比我原来的方法干净多了。