6

div をアニメーション化する無限ループを使用して jQuery 関数を実装しようとしています。私はそれを行う方法を理解できません。これは私のコードです:

$(document).ready(function () {
    $('#divers').animate({
        'margin-top': '90px'
    }, 6000).animate({
        'margin-top': '40px'
    }, 6000);
});
4

6 に答える 6

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
}); 
于 2012-11-13T19:08:39.853 に答える
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();
}); 
于 2012-11-13T19:07:48.507 に答える
2

コールバックを使用し.animate()て関数を「リコール」します。

jsBin デモ

$(function() {
  
  
  function loop(){
   $('#divers')
     .animate({marginTop:90},6000)
     .animate({marginTop:40},6000, loop); // callback
  }
  
  loop(); // call this wherever you want


}); 
于 2012-11-13T19:11:18.620 に答える
2

setInterval を使用する方法です。再帰が多すぎると、「スタック オーバーフロー」が発生します :-) 「Uncaught RangeError: 最大コール スタック サイズを超えました」

于 2015-01-21T20:17:50.023 に答える
1

どの間隔でどのメソッドを呼び出すかを指定するsetinterval関数を設定することもできます

$(function () { setInterval(fnName, 6000); });
于 2012-11-13T19:08:56.750 に答える
1

このanimate()関数には、アニメーションの終了時に呼び出す関数を受け取るオプションがあります。同じ電話をかけるだけで、できあがりです。

于 2012-11-13T19:07:10.683 に答える