如何循环jquery div动画?

Edu*_*zar 6 jquery loops jquery-animate

我正在尝试使用无限循环实现一个jQuery函数来设置div的动画.我无法弄清楚该怎么做.这是我的代码:

$(document).ready(function() {
    $('#divers').animate({'margin-top':'90px'},6000).animate({'margin-top':'40px'},6000);
});
Run Code Online (Sandbox Code Playgroud)

小智 9

将执行完整动画的代码放入函数中,然后将该函数作为回调参数传递给最后一个动画.就像是...

$(document).ready(function() {   
    function animateDivers() {
        $('#divers').animate(
            {'margin-top':'90px'}
            ,6000
        )
        .animate(
            {'margin-top':'40px'}
            ,6000
            ,animateDivers //callback the function, to restart animation cycle
        ); 
    }

    animateDivers(); //call, to start the animation
}); 
Run Code Online (Sandbox Code Playgroud)


ade*_*neo 5

$(document).ready(function() {
    function ani() {
        $('#divers').animate({
               'margin-top':'90px'
            },6000).animate({
               'margin-top':'40px'
           },6000, ani); //call the function again in the callback
        }); 
    }); 
    ani();
}); 
Run Code Online (Sandbox Code Playgroud)