我想随机选择每个元素的背景色。
但是,当我将它插入到以下内容中时,背景颜色就变得透明了:
{
modules.map((module, index) => (
<div className='carousel-module shadow'
style={{ background: "#" + Math.floor(Math.random()*16777215).toString(16)}}
>
<p className='module-element-text'>{module.name ? module.name : "N/A"}</p>
<p className='module-element-text'>{module.code ? module.code : "N/A"}</p>
<Button onClick={() => setShow(false)}
variant="success" className='modules-list-button'>
Load
</Button>
</div>
))
}很高兴听到你对如何使这件事起作用的建议。
发布于 2022-10-25 13:02:15
这是我用来生成十六进制颜色的函数。您的方法可能会在颜色中引入Alpha,从而获得透明
function randomHexColor() {
let toHex = function (n) {
let hex = n.toString(16);
while (hex.length < 2) { hex = '0' + hex; }
return hex;
};
let rr = toHex(Math.floor(Math.random() * 256));
let gg = toHex(Math.floor(Math.random() * 256));
let bb = toHex(Math.floor(Math.random() * 256));
return '#' + rr + gg + bb;
}https://stackoverflow.com/questions/74194296
复制相似问题