如何显示数组中的随机项目?

Jun Jung |
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)] 
});
}

在此处输入图片说明

嗨,所以这是一个迷你游戏,教用户如何回收利用。在屏幕截图中,用户需要单击黑洞/孔才能在黑洞内一个接一个地显示随机项(我尝试附加它,但之前不知道要创建什么)。我对如何实现这一目标感到困惑。我所拥有的似乎还不够。我感谢解决方案和提示。感谢您阅读我的帖子。

在此处输入图片说明

UPDATE: So the codes Elliot provided is working. 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. I hope this is not a complicated problem to solve T.T

Eliott Robson

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

https://jsfiddle.net/nxc5zpen/

It looks like the issues you might be having are due to SVG scaling. Here is a working JSFiddle with SVGs.

https://jsfiddle.net/Lhot76gm/1/

And here is another version using background images for the hole.

https://jsfiddle.net/j6373mvv/1/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

随机排列python数组中的某些项目

从PHP的JSON数组中获取随机项目

从JavaScript数组中获取随机项目

无法从数组中随机搜索项目

从JavaScript数组中获取随机项目

如何从数组中随机选择?

如何在PHP中显示数组中的项目数

Twig-如何随机化数组中的项目并循环?

如何以随机顺序显示对象中的项目并保持其各自的属性

如何过滤React中屏幕上显示的数组项目?

如何为数组中的每个项目分配随机颜色?

反复从javascript数组中获取随机项目?

显示数组中的随机内容

如何通过onClick随机使用array.map,useState,useEffect在React中显示项目?

如何使用React显示数组中特定数量的项目

如何在数组中放置随机数并在php中显示此数组

数组中的javascript随机项目?

从随机数组中检索随机数量的项目

如何从具有1000个项目的数组中随机选择一个项目?

用户执行单击时如何显示数组中的随机项目

如何随机显示数组?

php - 如何随机化数组中的项目

显示后如何停止数组中的项目以重复

如何随机显示数组中的字符串字符

如何从javascript中的数组中获取多个随机不同的项目?

如何从数组中随机选择一个项目?

如何从对象数组中显示单个项目 - 颤振

如何编写一个循环生成器,它从消息数组中随机选择项目,但在每个项目都显示一次之前不会重复单个项目?

如何在 html 中显示来自 Javascript 数组的随机引用?