有没有一种方法可以在没有PHP的情况下简化100多个图像数组

Is there a way to simplify a 100+ image array without PHP?

本文关键字:情况下 PHP 数组 图像 一种 方法 有没有      更新时间:2023-09-26

郑重声明,我对Java还不太精通,我只是从它开始。目前,我正在做一个小型个人项目,在转换到另一个文件夹之前,随机显示一个文件夹中的图像5秒钟。我想添加尽可能多的图像,但一旦我创建了一个超过100个对象的数组,管理它和文件就变得非常麻烦,尤其是如果我想开始重新排列文件或添加/删除一些文件。

我已经开始将文件重命名为序列号,以使随机循环发挥作用,这本身就是一件麻烦事,所以我很好奇是否有办法至少缩短数组并使其在文件中循环。或者可能有一个更简单的解决方案,它根本不需要数组,因为文件是用数字顺序命名的。

到目前为止,代码是这样的:

var delay=5000 //set delay in miliseconds
var curindex=0
var randomimages=new Array()
    randomimages[0]="1.png"
    randomimages[1]="2.jpg"
    randomimages[2]="3.png"
    randomimages[3]="4.png"
    randomimages[4]="5.png"
    randomimages[5]="6.png"
    randomimages[6]="7.png"
    randomimages[7]="8.jpg"
    randomimages[8]="9.png"
    randomimages[9]="10.png"
    // Cut out the 100 extra lines
    // Continues like this for multiple lines and variates between PNG and JPG images
    randomimages[103]="104.jpg"
var preload=new Array()
for (n=0;n<randomimages.length;n++)
{
    preload[n]=new Image()
    preload[n].src=randomimages[n]
}
document.write('<div><img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*(randomimages.length))]+'"></div>')
function rotateimage()
{
if (curindex==(tempindex=Math.floor(Math.random()*(randomimages.length)))){
curindex=curindex==0? 1 : curindex-1
}
else
curindex=tempindex
    document.images.defaultimage.src=randomimages[curindex]
}
setInterval("rotateimage()",delay)

感谢您的帮助。

试试这个代码:

var delay = 5000, //set delay in miliseconds
    count = 104,
    curindex = Math.floor(Math.random() * count),
    preload = [],
    img;
for (var n = 1; n <= count; n++) {
    img = new Image();
    img.src = n + '.png';
    preload.push(img);
}
document.write('<div><img name="defaultimage" src="' + (curindex + 1) + '.png"></div>')
function rotateimage() {
    var tempindex = Math.floor(Math.random() * count);
    if (curindex === tempindex) {
        curindex = (curindex === 0 ? 1 : curindex - 1);
    } else {
        curindex = tempindex;
    }
    document.images.defaultimage.src = (curindex + 1) + '.png';
}
setInterval("rotateimage()", delay);

没有必要像你说的那样有一个数组。

相关文章: