gpt4 book ai didi

javascript - 来自图像拉取的随机图像

转载 作者:行者123 更新时间:2023-11-30 20:05:19 24 4
gpt4 key购买 nike

我需要在重新加载页面时制作一个带有随机图像的 3x3 表格。

这是我的代码,但它更改了所有 9 个单元格。我真正想做的是为来自不同池的每个单元格拍照。例如单元格 1.1 只能从 img1 文件夹中拍照,1.2 只能从 img2 文件夹中拍照等。

<!DOCTYPE html>

<head>
<link rel="stylesheet" href="CSS.css" type="text/css" />
<title>Random</title>
</head>

<body onload="addEventListeners();">

<div id="table">
<table align=center>
<tr>
<td><img class="element" id="image1" src="img/1.jpg" /></td>
<td><img class="element" id="image2" src="img/2.jpg" /></td>
<td><img class="element" id="image3" src="img/3.jpg" /></td>
</tr>
<tr>
<td><img class="element" id="image4" src="img/4.jpg" /></td>
<td><img class="element" id="image5" src="img/5.jpg" /></td>
<td><img class="element" id="image6" src="img/6.jpg" /></td>
</tr>
<tr>
<td><img class="element" id="image7" src="img/7.jpg" /></td>
<td><img class="element" id="image8" src="img/8.jpg" /></td>
<td><img class="element" id="image9" src="img/9.jpg" /></td>
</tr>
</table>
</div>

<script>
var imagelist = ["img/1.jpg", "img/2.jpg", "img/3.jpg", "img/4.jpg", "img/5.jpg", "img/6.jpg", "img/7.jpg", "img/8.jpg", "img/9.jpg"];


function diffImage() {
var output = '';
whichImage = Math.floor(Math.random() * imagelist.length);
output = imagelist[whichImage];
imagelist.splice(whichImage, 1);
return output;
}

document.getElementById("image1").src = diffImage();
document.getElementById("image2").src = diffImage();
document.getElementById("image3").src = diffImage();
document.getElementById("image4").src = diffImage();
document.getElementById("image5").src = diffImage();
document.getElementById("image6").src = diffImage();
document.getElementById("image7").src = diffImage();
document.getElementById("image8").src = diffImage();
document.getElementById("image9").src = diffImage();
document.getElementById("image10").src = diffImage();
document.getElementById("image11").src = diffImage();
document.getElementById("image12").src = diffImage();

</script>
</body>

</html>

最佳答案

// you want three pools so let’s create three pools
var imagePools = [
// your images from folder 1
["https://placekitten.com/201/140", "https://placekitten.com/199/140", "https://placekitten.com/200/140"],
// your images from folder 2
["https://placekitten.com/g/201/140", "https://placekitten.com/g/199/140", "https://placekitten.com/g/200/140"],
// your images from folder 3
["https://placekitten.com/201/140", "https://placekitten.com/199/140", "https://placekitten.com/200/140"],
];


function diffImage(pool) {
// get the pool
var pool = imagePools[pool];
return pool[Math.floor(Math.random() * pool.length)];
}
var imgElementCount = 9;

for (var i = 1; i < imgElementCount + 1; i++) {
// this will always get you an integer of 1, 2 or 3
var pool = (i - 1) % 3;
document.getElementById("image"+i).src = diffImage(pool);
}
    <div id="table">
<table align=center>
<tr><td><img class="element" id="image1" src="img/1.jpg" /></td>
<td><img class="element" id="image2" src="img/2.jpg" /></td>
<td><img class="element" id="image3" src="img/3.jpg" /></td>
</tr>
<tr><td><img class="element" id="image4" src="img/4.jpg" /></td>
<td><img class="element" id="image5" src="img/5.jpg" /></td>
<td><img class="element" id="image6" src="img/6.jpg" /></td>
</tr>
<tr><td><img class="element" id="image7" src="img/7.jpg" /></td>
<td><img class="element" id="image8" src="img/8.jpg" /></td>
<td><img class="element" id="image9" src="img/9.jpg" /></td>
</tr>
</table>
</div>

这可能是一个可行的解决方案。解释在评论中。

关于javascript - 来自图像拉取的随机图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52995287/

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