生成一个随机的十六进制颜色代码。
使用 Math.random
生成一个随机的24位(6×4位)十六进制数。 使用位操作符,然后使用 toString(16)
将其转换为十六进制字符串。
const randomHexColorCode = () => { let n = ((Math.random() * 0xfffff) | 0).toString(16); return '#' + (n.length !== 6 ? ((Math.random() * 0xf) | 0).toString(16) + n : n); };
randomHexColorCode(); // "#e34155" randomHexColorCode(); // "#fd73a6" randomHexColorCode(); // "#4144c6"
更多代码 JavaScript 实用代码片段 请查看 https://www.7psus5.com/30-seconds-of-code/
最新评论
写的挺好的
有没有兴趣翻译 impatient js? https://exploringjs.com/impatient-js/index.html
Flexbox playground is so great!
感谢总结。
awesome!
这个好像很早就看到类似的文章了
比其他的教程好太多了
柯理化讲的好模糊…没懂