[英]How to display an random items from an array?
var items = ['imgs/garbagebag.svg', //[0]
'imgs/straw.svg', // [1]
'imgs/utensil.svg', // [2]
'imgs/chipbag.svg', // [3]
'imgs/eggs.svg', // [4]
'imgs/glasscup.svg', //[5]
'imgs/ketchupbottle.svg', //[6]
'imgs/jamjar.svg', //[7]
'imgs/milkbottle.svg', //[8]
'imgs/popbottle.svg', //[9]
'imgs/eggshell.svg', //[10]
'imgs/apple.svg', //[11]
'imgs/banana.svg', //[12]
'imgs/teabag.svg', // [13]
'imgs/leave.svg', // [14]
'imgs/jug.svg', // [15]
'imgs/tetrapak.svg', // [16]
'imgs/container.svg', // [17]
'imgs/plasticbottle.svg', // [18]
'imgs/can.svg', //[19]
'imgs/newspaper.svg', //[20]
'imgs/cerealbox.svg', // [21]
'imgs/book.svg', // [22]
'imgs/cardboard.svg', // [23]
'imgs/bag.svg' // [24]
]
// This is the black hole on the wall
var hole = document.getElementById("hole");
// starts functioning after 5 seconds
var start = setInterval(shuffle,5000);
// displays an random items from array one by one
function shuffle(){
hole.addEventListener("click",function(){
console.log("items");
items[Math.floor(Math.random()*items.length)]
});
}
Hi, so this is a mini-game that teaches users how to recycle. 嗨,所以这是一个教会用户如何回收的迷你游戏。 In the screenshot, users are required to click the black gap/hole in order to display the random items one by one inside the black hole (I tried appending it but I don't know what I should create before).
在屏幕截图中,用户需要点击黑色间隙/洞以便在黑洞内逐个显示随机项目(我尝试附加它但我不知道我之前应该创建什么)。 I am befuddled on how to achieve this.
我对如何实现这一点感到困惑。 What I have doesn't seem to be enough.
我所拥有的似乎还不够。 I appreciate solutions and tips.
我很感激解决方案和提示。 Thank you for reading my post.
感谢您阅读我的帖子。
UPDATE: So the codes Elliot provided is working. 更新:所以Elliot提供的代码正在运行。 But for some reasons, newly created + randomized images have no pixels!
但由于某些原因,新创建的+随机图像没有像素! I tried assigning new width and height + z-index with className in css but that is not doing anything either.
我尝试在css中使用className分配新的宽度和高度+ z-index,但这也没有做任何事情。 I hope this is not a complicated problem to solve TT
我希望这不是解决TT的复杂问题
It might be worthwhile restructuring your code slightly. 稍微重构代码可能是值得的。 It looks like you are grouping your items.
看起来你正在对你的物品进行分组。
var items = [{
image: "imgs/garbagebag.svg",
type: "paper"
}, {
image: "imgs/straw.svg",
type: "paper"
},
...]
This gives you a lot more flexibility in the meta data behind each item. 这使您在每个项目后面的元数据中具有更大的灵活性。
Then you need to add the events 然后你需要添加事件
var hole = document.getElementById("hole");
function showItem(item) {
var img = document.createElement("img");
img.setAttribute("src", item.image);
// store type as data attribute so we can access it later
img.setAttribute("data-type", item.type);
if (hole.childNodes[0]) {
hole.removeChild(hole.childNodes[0]);
}
hole.appendChild(img);
}
function showRandomItem() {
var item = items[Math.floor(Math.random()*items.length)];
showItem(item);
}
hole.addEventListener("click", function() {
showRandomItem();
});
Here is a working version of this code on JSFiddle 这是JSFiddle上此代码的工作版本
https://jsfiddle.net/nxc5zpen/ https://jsfiddle.net/nxc5zpen/
It looks like the issues you might be having are due to SVG scaling. 看起来您可能遇到的问题是由于SVG扩展。 Here is a working JSFiddle with SVGs.
这是一个有SVG的JSFiddle。
https://jsfiddle.net/Lhot76gm/1/ https://jsfiddle.net/Lhot76gm/1/
And here is another version using background images for the hole. 这是另一个使用孔的背景图像的版本。
https://jsfiddle.net/j6373mvv/1/ https://jsfiddle.net/j6373mvv/1/
这是如何从数组中获取项目
var randItem = items[Math.floor(Math.random() * items.length)];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.