0

一連の div をランダムにシャッフルする小さなスクリプトを作成しました。これは期待どおり (または期待どおり) に機能します。

私の問題は実装にあります。div をフェードアウトし、シャッフルして、再びフェードインしたい。私が見つけたのは、関数 moveBox() が任意のアニメーションと同時に実行されることです。アニメーションのすべての要素 (fadeOut、delay、fadeIn) へのコールバック関数として呼び出してみましたが、常に同じ効果がありました - アニメーション中に div のシャッフルと再配布が発生するため、表示されます。

div が非表示のときにシャッフルを発生させるソリューション (var ts=timeOut...) がありますが、これが最適なソリューションであるとは確信していません。

関数の実行順序を制御する方法と、それらを同時に実行するか順番に実行するかを知りたいです。私のコード:

<style>
    .tester{
        float:left;
        width:100px;
        height:100px;
        margin:5px;
        }
    .tester p{text-align:center;
        margin-top:20px;
    }
    .one{background-color:red;}
    .two{background-color:yellow;}
    .three{background-color:teal;}
    .four{background-color:blue;}
    .five{background-color:green;}
    .six{background-color:silver;}
</style>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    jQuery(document).ready(function(){
        var anibase=jQuery(".tester").length;
        jQuery(".tester").fadeOut(1000);
        function moveBox(){
            function shuffle(){
                for (i = 0; i <= (anibase - 2); i++) {
                    mover = Math.floor(Math.random() * (anibase - i));
                    if (mover != 0) {
                        mover = mover + i;
                        jQuery(".tester:eq(" + mover + ")").insertBefore(".tester:eq(" + i + ")");
                    };
                };
            };
            jQuery(".tester").fadeOut(1500).delay(500).fadeIn(1500);
            var ts = setTimeout(shuffle,1500);
            var t=setTimeout(moveBox,5000);
        };
        moveBox();
    });

</script>
<body>
    <div class="tester one">
        <p>box 1</p>
    </div>
    <div class="tester two">
        <p>box 2</p>
    </div>
    <div class="tester three">
        <p>box 3</p>
    </div>
    <div class="tester four">
        <p>box 4</p>
    </div>
    <div class="tester five">
        <p>box 5</p>
    </div>
    <div class="tester six">
        <p>box 6</p>
    </div>
</body>

前もって感謝します

4

1 に答える 1

0

JQuery には関数をネストする機能があり、実行順序を効果的に制御します。

まず、Javascript で関数をネストすることは有効ですが、この場合は必須ではありません。次のコードを検討してください。

<script type="text/javascript">

  jQuery(document).ready(function(){
    moveBox();  //call the function that will do the work and then setTimeout
  });



  function moveBox(){
    jQuery(".tester").fadeOut("slow",function(){
      shuffle();
      $(this).fadeIn("slow");
      return;
    });
    var t=setTimeout(moveBox,5000);
  }

  function shuffle(){
    var anibase=jQuery(".tester").length;
    for (i = 0; i <= (anibase - 2); i++) {
      mover = Math.floor(Math.random() * (anibase - i));
      if (mover != 0) {
        mover = mover + i;
        jQuery(".tester:eq(" + mover + ")").insertBefore(".tester:eq(" + i + ")");
      }
    }
  }
</script>

ここで注目すべき重要な部分は次のとおりです。

jQuery(".tester").fadeOut("slow",function(){
  shuffle();
  $(this).fadeIn("slow");
  return;
});

これにより、jQuery に次のように指示されます。

  1. .tester 要素をフェードアウトする
  2. フェードアウトが完了したら、shuffle() を実行します。
  3. 次に、要素をフェードインします

この例については、fadeOutのドキュメントを参照してください。いくつかの例では、チェーンされたイベントが示されています。

また、関数定義を分けておくことで、読みやすくなります。

于 2011-05-08T13:19:32.830 に答える