js实现随机颜色

方法一:

function coloring() {let r = Math.floor(Math.random() * 255)let g = Math.floor(Math.random() * 255)let b = Math.floor(Math.random() * 255)return `rgb(${r}, ${g}, ${b})`
}
document.body.style.background = coloring()

方法二:

function coloring() {return '#' + (Math.floor(Math.random() * 0xffffff).toString(16).padStart(6, '0'))
}
document.body.style.background = coloring()

方法三:

function coloring() {return new Array(4).fill('#').reduce((prev, cur) => {return prev + Math.floor(Math.random() * 256).toString(16).padStart(2, '0')})
}
document.body.style.background = coloring()

方法四:

function coloring() {return new Array(7).fill('#').reduce((prev, cur) => {return prev + Math.floor(Math.random() * 16).toString(16)})
}
document.body.style.background = coloring()


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部