一定時間ごと画像を入れ替える


動作ブラウザ 【 IE:4.0  NN:3.0
Internet Explorer Netscape Navigator DreamPassport iCab
3.0x 4.0x 4.5 5.0x 5.5 2.0x 3.0x 4.0x 4.x 6.0 2 3 2.x
Windows × - × - -
Macintosh × - × - -
UNIX - - - - - × - - -
Dreamcast - - - - - - - - - - -

ポイント count++; count %= 4; document.btn.src = count + ".gif"; <body onLoad="setInterval('changeImg()',2000)">
説  明 一定時間ごと画像を入れ替えるにはsetTimeout()かsetInterval()を使います。入れ替える画像のファイル名を工夫し「連番+拡張子」にしておきます。count++でカウントを増やし、count % = 4で剰余を求めます。4は表示する画像の枚数です。10枚あるならば10となります。
サンプル <html> <head> <title>一定時間ごと画像を入れ替える</title> <script language="JavaScript"><!-- count = 0; function changeImg() { count++; count %= 4; document.btn.src = count + ".gif"; } // --></script> </head> <body onLoad="setInterval('changeImg()',2000)"> <img src="1.gif" name="btn"> </body> </html>
補足説明 なし

■サンプルスクリプトを実行する >>実行
■各ブラウザでの動作結果を見る >>View!

写真素材 PIXTA