blog.sowatchasayin

Rails etc.

javascriptを用い、ランダムに画像を表示せよ。連続して同じ画像を表示してはならない。

でけた♪でけた♪

images = ["back1.JPG","back2.JPG","back3.JPG","back4.JPG","back5.JPG"]
rand = function(i){return Math.floor( Math.random() * i );}
setRandomImage = function(){
function another_image(){
r = ("url(" + images[rand(images.length)] + ")");
return document.getElementById("どこか").style.backgroundImage != r ? r : another_image();
};
document.getElementById("どこか").style.backgroundImage = another_image();
};

これをonMouseOverとかで読んであげよう。
サンプル http://so-wat-cha-say.in/
documentオブジェクトに毎回アクセスしてるけど、いいんだ。

No comments:

Post a Comment

Author

Fujimura Daisuke
http://fujimuradaisuke.com

Labels