我有一个包含10个图像的文件夹(img-1.jpg,img-2.jpg等)。目前,我在一个网站上展示了6张图片,并且尝试将6张图片中的一张随机替换为页面上未显示的另一张图片。
我有一个所有图像的数组(fullList),然后生成并显示了当前显示的图像(currentList)。
我遇到的问题是循环遍历currentList数组,直到找到当前不在currentList数组中的随机生成的项。然后,我将从页面中选择一个随机图像并进行更改。
我现在所拥有的:
function sawpHeadshots() {
var fullList = [1,2,3,4,5,6,7,8,9,10];
var currentList = [];
$('#headshots li').each(function() {
currentList.push(parseInt($(this).css('background-image').replace(/\D+/, '')));
});
function generateRandom() {
return fullList[Math.floor(Math.random() * fullList.length)];
}
var rand = generateRandom();
/* not sure how to proceed at this point. */
}
创建该数组的副本,然后对其随机排序,并在创建数组时将其从数组中删除。无需继续生成随机数或跟踪使用了什么。
var fullList = [1,2,3,4,5,6,7,8,9,10];
var random = fullList.slice(0).sort(function() {
return .5 - Math.random();
});
//get value with pop()
var current = random.pop();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句