最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - show random image from array in javascript - Stack Overflow

programmeradmin2浏览0评论

I want to select random images from imgArray to print 16 different images at the same time but can't fill the imgRandom function.

img = new imgArray(7);
img[0]='1.png';
img[1]='2.png';
img[2]='3.png';
img[3]='4.png';
img[4]='5.png';
img[5]='6.png';
img[6]='7.png';
img[7]='8.png';

var rand=imgArray[math.floor(math.random*imgArray.length)];

function imgRandom(){

}

I want to select random images from imgArray to print 16 different images at the same time but can't fill the imgRandom function.

img = new imgArray(7);
img[0]='1.png';
img[1]='2.png';
img[2]='3.png';
img[3]='4.png';
img[4]='5.png';
img[5]='6.png';
img[6]='7.png';
img[7]='8.png';

var rand=imgArray[math.floor(math.random*imgArray.length)];

function imgRandom(){

}
Share Improve this question edited Dec 22, 2012 at 16:25 Pebbl 36k6 gold badges64 silver badges65 bronze badges asked Dec 22, 2012 at 16:06 gunericinargunericinar 331 gold badge1 silver badge6 bronze badges 3
  • You have just 8 elements in the array, how will you show 16 images? – closure Commented Dec 22, 2012 at 16:19
  • same image can be seen twice – gunericinar Commented Dec 22, 2012 at 16:20
  • As it is random, will it look ok if same two images are side by side. Just my thought... – closure Commented Dec 22, 2012 at 16:22
Add a comment  | 

3 Answers 3

Reset to default 10

Try this function

function getRandomImage(imgAr, path) {
    path = path || 'images/'; // default path here
    var num = Math.floor( Math.random() * imgAr.length );
    var img = imgAr[ num ];
    var imgStr = '<img src="' + path + img + '" alt = "">';
    document.write(imgStr); document.close();
}

Call the function

getRandomImage(ARRAY-VARIABLE, '/images/')

Refer LIVE DEMO. You will see change in image for every refresh

Use the following code:

var imgArray = ['1.png', '2.png', '3.png', '4.png', '5.png', '6.png', '7.png', '8.png'];
var basePath="YOUR_FOLDER_PATH_HERE";

function imgRandom() {
    for (var i = 0; i < 18; i++) {
        var rand = imgArray[Math.floor(Math.random() * imgArray.length)];
        var image = new Image();
        image.src = basePath+rand;
        document.body.appendChild(image);
    }
}

Live Demo | Source

There are some things in your code i would like to point out

  • There is no imgArray in javascript per default (maybe its a constructor of your's)
  • You want an Array

  • Javascript is case sensitiv, and the Math object in js has an capital M

    • math -> Math
  • And the random propertie of the Math object, has to be invoked to return a random number
    • random -> random()
    • Math.random()

But to your Question

  1. functions take parameters, so could take one param for an array of images

    function (imgArr) {...
    With this you have access to the passed parameter throug imgArr

  2. function can return values, so taken Math.random we can go on

    function (imgArr) { return imgArr[Math.floor(Math.random() * imgArr.length)] }

  3. You don't have to "Dim" Array, and predefine the Elements you gonna put in, you can just do sth. like

    var imgArr = ["img01.png","img02.png",...]
    And var arr=[] would be equals var arr = new Array()

  4. So calling this function with your Array couldlook like


  var img = ["1.png", "2.png", "3.png", "4.png", "5.png", "6.png", "7.png", "8.png"]
     function imgRandom(imgArr) {
        return imgArr[Math.floor(Math.random() * imgArr.length)];
    }
 console.log(imgRandom(img));
发布评论

评论列表(0)

  1. 暂无评论