更好的实现摇动动画的方法?

Abe*_*ler 2 javascript jquery

我正在尝试使用jQuery实现"摇动动画".我们的想法是,你可以制作一个元素震动来引起人们的注意.以下是我提出的建议:

    function DrawAttention(item, count)
    {
        $(item).animate({top: '+=5'}, 50,
            function(){
                $(item).animate({top: '-=10'}, 100,
                    function(){
                       $(item).animate({top: '+=5'}, 50,
                            function(){
                                if(count>0)
                                {
                                    DrawAttention(item,count-1);
                                }
                            }); 
                    });
            });

    }
Run Code Online (Sandbox Code Playgroud)

我觉得这有点冗长,并且想知道是否有人能看到更优雅的方式来实现我想要的东西.

小提琴这里.

pck*_*ill 7

function DrawAttention(item, count)
{
    $(item)
        .animate({top: '+=5'}, 50)
        .animate({top: '-=10'}, 100)
        .animate({top: '+=5'}, 50, function(){
            if(count > 0){
              DrawAttention(item,count-1);
            }
        });
}
Run Code Online (Sandbox Code Playgroud)

  • 你错了,在http://api.jquery.com/animate/上搜索"链式动画".但它确实无法正常工作,我错误地输入了if语句中的`>`符号.如果您将当前代码粘贴到小提琴,它将起作用. (2认同)