const images = ['https://picsum.photos/id/9/5000/3269', 'https://picsum.photos/id/11/2500/1667', 'https://picsum.photos/id/21/3008/2008'];
// pick a random item from the list
const rand = Math.floor(Math.random()*images.length);
const src = images[rand];
console.log(rand, src);
// just use the src value in your component, it will get a new one each time it is mounted or rendered
// or, build a DOM element if not in a framework
const img = document.createElement('IMG');
img.src = src;
img.style.width = "100vw";
document.body.appendChild(img);
1条答案
按热度按时间ee7vknir1#
您应该提供一些更相关的代码,并且通常在SO中要详细说明您尝试做了什么以及它让您走了多远。如果我理解你的问题足够,你有一个预先存在的url数组,并希望每次显示页面或呈现组件时简单地随机选择一个。