在动画回调完成之前触发 jquery 循环进行迭代?

发布于 2024-09-12 06:23:27 字数 343 浏览 4 评论 0原文

我正在尝试制作一个顺序动画,其中循环遍历元素列表 .post 并缓慢淡出它们。困难的部分是让下一个迭代在上一个迭代完成淡入之前开始淡入。到目前为止,我所拥有的只是一个简单的顺序动画器,可以将它们一个接一个地淡入淡出。

$(".post").hide();
var posts = $(".post"),
    i = 0;
(function() {
  $(posts[i++]).fadeIn('slow', arguments.callee);
})();

有谁知道我如何更改此设置以允许 .post 在前面的 elem/s 完成之前淡入()?

I am trying to make a sequential animation, where a loop iterates through a list of elements .post's and fades them in slow. The difficult part is having it so that the next iteration begins fading in before the last one has finished fading. All I have so far is a simple sequential animator that fades them in one after the other.

$(".post").hide();
var posts = $(".post"),
    i = 0;
(function() {
  $(posts[i++]).fadeIn('slow', arguments.callee);
})();

Does anyone know how I could change this to allow .post to fadeIn() before the previous elem/s have finished?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奈何桥上唱咆哮 2024-09-19 06:23:27

使用 each() 迭代它们,并为每一个使用 setTimeout(),将动画的持续时间乘以当前的 index每个。

var posts = $(".post").hide();

  // Holds reference to the index of the current iteration
  // ------------------v
posts.each(function( index ) {
    var $th = $(this);
    setTimeout(function() {
        $th.fadeIn('slow');   // Removed arguments.callee
    }, index * 300);
});

因此,每个 setTimeout() 的持续时间为 n*600,这应该会给您带来您想要的效果。

顺便说一句,如果您不需要 posts 变量用于任何其他目的,您可以消除它并将 .each() 链接在 之后>.hide(),如 $(".post").hide().each(func...)


编辑: 我有一个错误。我在 setTimeout() 中使用了 this,它具有不同的值。编辑以传递正确的值。

编辑:误读了问题。将 setTimeout() 的持续时间更改为 300 以在动画中提供部分重叠。

Iterate over them using each() and use a setTimeout() for each one, multiplying the duration of the animation by the current index in the each.

var posts = $(".post").hide();

  // Holds reference to the index of the current iteration
  // ------------------v
posts.each(function( index ) {
    var $th = $(this);
    setTimeout(function() {
        $th.fadeIn('slow');   // Removed arguments.callee
    }, index * 300);
});

So each setTimeout() will have a duration of n*600, which should give you the effect you want.

By the way, if you don't need the posts variable for any other purpose, you can eliminate it and chain the .each() after the .hide(), as in $(".post").hide().each(func...)


EDIT: I had an error. I was using this inside the setTimeout() which has a different value. Edited to pass in the correct value.

EDIT: Mis-read the question. Changed the duration of setTimeout() to 300 to give a partial overlap in the animations.

自由范儿 2024-09-19 06:23:27

与帕特里克的解决方案类似,但我认为

(function() {
  $(".post").hide().each(function(index){
    $(this).delay(index * 300).fadeIn('slow');
  });
})();

演示300) 更干净一些是延迟的持续时间,其中 'slow' 是淡入淡出的持续时间

Similar to patrick's solution, but a little cleaner in my opinion

(function() {
  $(".post").hide().each(function(index){
    $(this).delay(index * 300).fadeIn('slow');
  });
})();

demo, the 300 is the duration of the delay where as the 'slow' is the duration of the fade

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文