特定の関数セットを書くのに問題があります。レスポンシブ サイトのビジュアル スポンサー バーに取り組んでいます。5 つの div に分割された約 20 個のスポンサー ロゴがあり、ランダムに循環させる必要があります。各 div に画像が 2 つしかない場合、私が始めたコードはシームレスに機能します。
画像はランダムにフェードインおよびフェードアウトします。ただし、フリップフロップ効果以上の機能が必要です。つまり、div ごとにローテーター画像の配列を作成し、ランダムに 1 つを選択する関数を記述し、その配列内の表示されている画像をオフにしてから、選択した画像をフェードインする必要があります。
私はこれを何度も繰り返し、画像がリスト形式でレイアウトされている場合に機能するコードを見つけましたが、ブラウザウィンドウのサイズに応答するときに画像をフロートしてラップする必要があります. どんな助け(および説明)も大歓迎です。
ありがとうございました!
フィドル全体へのリンクは以下にありますが、最初の (5 つの) div は次のようになります。
<div class="rotator">
<img src="http://www.fourtownfair.com/images/sponsors/agway.png" class="rotator-image" />
<img src="http://www.fourtownfair.com/images/sponsors/robertrookey.png" class="rotator-image"/>
<img src="http://www.fourtownfair.com/images/sponsors/fairviewfarms.png" class="rotator-image" />
<img src="http://www.fourtownfair.com/images/sponsors/redrobin.png" class="rotator-image" />
</div>
<div class="rotator">
<img src="http://www.fourtownfair.com/images/sponsors/equestriancollection.png" class="rotator-image" />
<img src="http://www.fourtownfair.com/images/sponsors/salmonbrook.png" class="rotator-image" />
<img src="http://www.fourtownfair.com/images/sponsors/smyth.png" class="rotator-image" />
<img src="http://www.fourtownfair.com/images/sponsors/conlinfarm.png" class="rotator-image" />
</div>
Javascript:
$(function() {
//Timeout are called once and only once, you need to use Interval to repeat the call :-)
$(document).ready( function() {
//Interval with millisecond delay, 2000 = every 2 seconds, always divide by 1000 to get the time.
setInterval(function(){rotateImages();}, 2000);
});
//Breaking this function out, to make the interval statement more readable
var rotateImages = function() {
//Another thing I do to enhance readability, break the collection into a variable
var rotatorArray = $(".rotator");
//We've got the array of rotator blocks, select a random one by length (out of 5 in this case)
var rand = Math.floor(Math.random() * rotatorArray.length);
//If the topmost randomly selected image is hidden, fade in, if it's visible, fade out
if($(rotatorArray[rand].children[0]).css('display') == "none")
{
$(rotatorArray[rand]).children(".rotator-image").fadeIn();
}
else
{
$(rotatorArray[rand]).children(".rotator-image").fadeOut();
}
};
});