原生js生成随机颜色

原生js生成随机颜色

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<style>
    #demo{
        width: 600px;
        height: 600px;
    }
</style>
<body>
    <div id="demo"></div>
<script>
    var div = document.getElementById("demo");
    randomColor(div);
    function randomColor(div) {
        r = Math.ceil(Math.random()*255);
        g = Math.ceil(Math.random()*255);
        b = Math.ceil(Math.random()*255);
        a = (Math.random()).toFixed(1);
        div.style.background = "rgb(" + r + "," + g + "," + b + "," + a + ")";
}
</script>
</body>
</html>

你可能感兴趣的:(原生js)