さて、他のプログラミング タスクと同じように、このような作業を単純なステップに分割する必要があります。この場合、おそらく次のようなものです。
- ページが読み込まれると、最初の画像のみが表示されます。これを行うには
display:none
、最初の画像を除くすべての画像に CSS ルールを設定する必要があります。hide
これは、 というクラスを作成し、それを HTML に適用することで簡単に実現できます。JS 経由でこれを管理することもできますが、ユーザーのインターネット接続によってはバグが発生する可能性があります...
- 5 秒ごとに現在の画像をフェードアウトさせ、次の画像をフェードインさせます。
- 最後の画像を表示している場合は、リストの最初の画像に戻るようにしてください。
必要な作業はこれでほぼすべてなので、コードを書きましょう。
まず、コンテナの ID を使用するようにマークアップをリファクタリングし、最初の画像を除くすべての画像に CSS クラスを追加します。
<div id="img_container">
<img src="images/1.png" class="image_wall_thumbnail" />
<img src="images/2.png" class="hide image_wall_thumbnail" />
<img src="images/3.png" class="hide image_wall_thumbnail" />
<img src="images/4.png" class="hide image_wall_thumbnail" />
<img src="images/5.png" class="hide image_wall_thumbnail" />
<img src="images/6.png" class="hide image_wall_thumbnail" />
</div>
次に、少し CSS を書きましょう。
.hide {
display:none;
}
さて、JSを書く「トリッキーな」部分です:
$(function() {
//cache dom elements and set init vars
var $img = $('#img_container').find('img'),
max = $img.length, //how many images you have in the container
current = 0; //we will start the script at the first item
//Every five seconds, run the code within the handler
setInterval(function(){
$($img[current]).fadeOut('fast', function(){
determineIndex(); //Update the index of the image in the img array
$($img[current]).fadeIn();
});
}, 5000);
function determineIndex () {
current = (current === max - 1) ? 0 : (current + 1);
}
});
これがデモです!http://jsfiddle.net/T2nzh/
JavaScript の仕組みについて質問がある場合は、コメントしてください。:)
編集:わかりました。画像ソースをランダムに交換したい場合は、これをチェックしてください。必要な html:
<div id="img_container">
<img src="images/1.png" style="background:red" class="image_wall_thumbnail" />
<img src="images/2.png" style="background:silver" class="image_wall_thumbnail" />
<img src="images/3.png" style="background:purple" class="image_wall_thumbnail" />
<img src="images/4.png" style="background:yellow" class="image_wall_thumbnail" />
<img src="images/5.png" style="background:green" class="image_wall_thumbnail" />
<img src="images/6.png" style="background:blue" class="image_wall_thumbnail" />
</div>
<div id="img_spares" style="display:none;">
<img src="images/7.png" style="background:magenta" class="image_wall_thumbnail" />
<img src="images/8.png" style="background:brown" class="image_wall_thumbnail" />
</div>
そしてJS:
$(function() {
//cache dom elements and set init vars
var $img = $('#img_container'),
$spares = $('#img_spares'),
max = $img.find('img').length,
spares_max = $spares.find('img').length;
//Every five seconds, run the code within the handler
setInterval(function(){
$($img.find('img')[randomIndex(0,max)]).fadeOut('fast', function(){
var $el = $(this),
el_source = $el.attr('style'),
$swap = $($spares.find('img')[randomIndex(0,spares_max)]),
swap_source = $swap.attr('style');
$el.attr('style', swap_source).fadeIn();
$swap.attr('style', el_source);
});
}, 1000);
function randomIndex (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
});
そしてデモ: http://jsfiddle.net/T2nzh/3/