思路
将hex十六进制的每一位逐一列出,然后循环六次不断随机抽取里面任意一个字符添加到结果的最后面,最后再返回。
/* 随机背景颜色 */
(function () {
const countdown = document.querySelector('.countdown');
function getRandomColorHex() {
const str_last = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
const str_start = '#';
let result = str_start;
for (let i = 0; i < 6; i++) result += str_last[Math.floor(Math.random() * str_last.length)];
return result;
}
countdown.style.backgroundColor = getRandomColorHex();
})();