2012-02-22 13 views
0

2つのループを1つずつ表示する必要があります。jQueryでループを作成する方法

HTML テキスト その他のテキスト

jQueryの

$('#1').animate({opacity:0},500) 
    .animate({opacity:1},500) 
    .delay(2000) 
    .animate({opacity:0},500); 

の間にここに行くべきですか?だから、第一、私たちは#1のためのアニメーションを実行するだけにして#1 DIV

$('#2').animate({opacity:0},500) 
    .animate({opacity:1},500) 
    .delay(2000) 
    .animate({opacity:0},500); 

そして、どのようにこのうち無限ループを作るためにはアニメーションを開始しますか?

答えて

1

無限ループの場合、各divのアニメーションを独自の関数に入れて、その関数を他のアニメーションの最後の完全なコールバックから呼び出すことができます。 (説明よりも、表示が簡単:)

function do1() { 
    $('#1').animate({opacity:0},500) 
     .animate({opacity:1},500) 
     .delay(2000) 
     .animate({opacity:0},500,function() { 
      do2(); 
     }); 
} 
function do2() { 
    $('#2').animate({opacity:0},500) 
      .animate({opacity:1},500) 
      .delay(2000) 
      .animate({opacity:0},500,function() { 
      do1(); 
     }); 
} 

​do1();​ 

デモ:http://jsfiddle.net/nnnnnn/HQ5ys/1

それとも、自分自身を呼び出す単一の関数内のコードを置く:

function myAnimate() { 
    $('#1').animate({opacity:0},500) 
     .animate({opacity:1},500) 
     .delay(2000) 
     .animate({opacity:0},500,function() { 
      $('#2').animate({opacity:0},500) 
      .animate({opacity:1},500) 
      .delay(2000) 
      .animate({opacity:0},500,function() { 
       myAnimate(); 
      }); 
} 

​myAnimate(); 

http://jsfiddle.net/nnnnnn/HQ5ys/2/

1

コールバックはanimate()です。アニメーションが終了するとコールバックが実行されます。

$('#one').animate({}, 'fast', function(){ 
    //Callback 
    $('#two').animate({ }); 
}); 
+0

これは動作します。これをループするのはどうですか? –

+0

最も単純なケースでは、関数内にすべてを入れ、 'setInterval(func、1000); // 1秒ごとに実行する。しかし、あなたは 'queue()'と 'dequeue()'を調べたいかもしれません – elclanrs

-1

のような完全なコールバック関数を使用してください:あなたは、このためのjQueryのとき、その関数を使用することができます

 

setInterval(function() { 
$("#1").animate({opacity:0}, 500, function() { 
    $("#2").animate({opacity:0},500); 
}), 5000); //runs every 5 secs 
 
+0

ありがとう!これをループする方法を知っていますか?何度も何度も繰り返されますか? –

+0

編集したコードを参照してください、あなたはsetIntervalを使用することができます –

+0

ありがとうございました!できます。 –

0

$.when(
    function(){ 
     $('#1').animate({opacity:0},500) 
      .animate({opacity:1},500) 
     .delay(2000) 
     .animate({opacity:0},500);}) 
.then(function(){ 
    $('#2').animate({opacity:0},500) 
    .animate({opacity:1},500) 
    .delay(2000) 
    .animate({opacity:0},500); 
}); 
関連する問題