我很抱歉,但我是一个严重的新手。
有人可以告诉我如何使用循环加载图像吗?
即重写以下类型的代码以使用循环来自动执行该过程。
function loadimages() {
pic00 = new Image;
pic00.src = "images/IMG_0019.jpg";
pic01 = new Image;
pic01.src = "images/IMG_0020.jpg";
pic02 = new Image;
pic02.src = "images/IMG_0021.jpg";
pic03 = new Image;
pic03.src = "images/IMG_0022.jpg";
pictures = new Array(4);
pictures[0] = pic00;
pictures[1] = pic01;
pictures[2] = pic02;
pictures[3] = pic03;
}
我看过许多描述类似事情的帖子,但恐怕我太笨了,无法理解它们。任何帮助表示赞赏。
问候
最佳答案
这样做会:
var URLs = [
"http://placehold.it/128x128.png/f00/400?text=Red",
"http://placehold.it/128x128.png/0f0/040?text=Green",
"http://placehold.it/128x128.png/00f/004?text=Blue",
"http://placehold.it/128x128.png/ff0/440?text=Yellow"
];
var imgs = URLs.map(function(URL) {
var img = new Image();
img.src = URL;
document.body.appendChild(img);
return img;
});
关于javascript - 在 Javascript 中使用循环创建和加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16825964/