0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

画像をランダムで表示させる

Posted at

##仕様
ロードの度に表示される画像がランダムで切り替わるようにしたい。

##js

js
var randomImg = function () {
    var $target = $('[data-randomimg]'),
        //表示させたい画像の個数を配列に入れる
        ary = [1,2,3], 
        //ランダムで配列内の数字を取得
        i = ary[Math.floor(Math.random() * ary.length)],
        src;
    if(!$target.length) return;
    src = 'img/img_' + i + '.gif';
    $target.attr('src',src);
};

$(function () {
    randomImg();
});

##まとめ
わざわざ配列に画像の個数を入れるのがなんとも。

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?