一連の 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>
前もって感謝します