JS实现生成随机颜色 rgb与16进制
function getcolor(data){
if(data){
let str = '#'
let arr=['1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','f','g','h','i','j','k','l','m','n']
// 利用佛如循环随机抽6次
for(let i =1;i<=6;i++){
// 每次随机抽一个
let random = Math.floor(Math.random() * arr.length)
str +=arr[random]
}
return str;
}else{
let r = Math.floor(Math.random() * 256)
let g = Math.floor(Math.random() * 256)
let b = Math.floor(Math.random() * 256)
return `rgb(${r},${g},${b})`
}
}
console.log(getcolor(true)); // 16进制
console.log(getcolor(false)); // rgb
主题有任何问题,欢迎加群 xxxxxx 。(相对紧急的问题请私聊群主。)
本文链接:
https://mubanniao.com/wordpresscourse/47/ ,转载请注明出处。