gpt4 book ai didi

javascript - 循环遍历javascript中的图像数组

转载 作者:行者123 更新时间:2023-12-02 16:47:25 24 4
gpt4 key购买 nike

我正在尝试循环遍历图像数组,但似乎无法跳过图像 2。

当最后一张图像通过时,数组也应该循环回 1...

    var WorkArray = new Array('work/01.png', 'work/02.png', 'work/03.png', 'work/04.png');

var nelements = WorkArray.length;

preload_image_object = new Image();
var i = 0;
for(i=0; i<=nelements; i++) {
preload_image_object.src = WorkArray[i];
}

function cC() {
var nelements = WorkArray.length;
var i = 0;
for(i=0; i<=nelements; i++) {
nelements = WorkArray[i];
}
document.getElementById("work").style.backgroundImage="url('"+WorkArray[i]+"')";
}

最佳答案

可以保存当前文件并使用取模循环运行。

它看起来像这样:

var WorkArray = new Array('work/01.png', 'work/02.png', 'work/03.png', 'work/04.png');
var currentImage = 0

function nextImage(){
currentImage = (currentImage + 1) % WorkArray.length;
document.getElementById("work").style.backgroundImage="url('"+WorkArray[currentImage]+"')";
}

关于javascript - 循环遍历javascript中的图像数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26993177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com