jquery顺序动画

标签 jquery animation sequential

我正在尝试复制这个动画

http://tympanus.net/Tutorials/AnimatedContentMenu/

我无法为菜单项设置动画,只能按顺序向上滑动

    $('#bar').animate(
    {width: '100%'},
    {duration: 500,
    specialEasing: {width: 'linear'},
    complete: function() {
        $('li').each( function() {
                $(this).animate(
                    {top:'0px'},
                    {queue: true, duration: 200, specialEasing: {top: 'easeOutBack'},
                }); 
        });
    }
});

这样菜单项就会同时动画......出了什么问题?

最佳答案

由于队列是每个元素,所以实际上没有任何东西在排队,这就是为什么它们同时都具有动画效果。不过,有几种方法可以做到这一点。对于少量项目执行此操作的一种简单方法是使用 .delay() ,像这样:

$('#bar').animate(
    {width: '100%'},
    {duration: 500,
    specialEasing: {width: 'linear'},
    complete: function() {
        $('li').each(function(i) {
            $(this).delay(i*200).animate(
                {top:'0px'},
                {queue: true, duration: 200, specialEasing: {top: 'easeOutBack'},
            }); 
        });
    }
});

You can test a demo out here .

它的作用是延迟动画 200ms * .each() 中元素的索引迭代,所以第一个动画是即时的,第二个动画是 200 毫秒后,接下来的 200 毫秒,等等。我建议这样做的原因是你没有使用 200 毫秒的延迟,你可以使用较小的数字并且动画有一点重叠,这似乎就是您想要的效果。

执行此操作的另一种方法(更详细,仅按顺序,并且不允许重叠)是使用 .queue()构建您自己的,例如:

$('#bar').animate(
    {width: '100%'},
    {duration: 500,
    specialEasing: {width: 'linear'},
    complete: function() {
        $('li').each(function() {
            $(document).queue(function(n) {
              $(this).animate(
                {top:'0px'},
                {queue: true, duration: 200, specialEasing: {top: 'easeOutBack'},
                complete: n //call the next item in the queue             
              }); 
            });
        });
    }
});

You can test that version out here .

关于jquery顺序动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6562231/

相关文章:

javascript - pjax加载的js文件无法使用jquery

javascript - 加载页面内容而不重新加载横幅

android - 在android中循环图像帧

c - 关于 'string' 和 'int' 之间转换的奇怪警告

css - 在纯 css3 循环中按顺序为元素设置动画

javascript - Square 是否正确继承自 Rectangle?

javascript - 没有按照我想要的方式获取 json

jquery - 加载页面时从右到左动画 3 个 DIV。

ios - 试图找出如何在 Swift 2.0 中加载多个图像序列

shell - 在文件名末尾添加序列号 - Shell 脚本