JavaScript产生随机颜色

2022-04-12 20:30:29 浏览数 (1)

代码语言:javascript复制
 //获取rgb类型的颜色  IE7不支持
            function randomColor(){
                var r = Math.floor(Math.random()*256);
                var g = Math.floor(Math.random()*256);
                var b = Math.floor(Math.random()*256);
                return "rgb(" r "," g "," b ")";
            }
             
            //获取十六进制颜色
            function randomColor1(){
                var r = Math.floor(Math.random()*256);
                var g = Math.floor(Math.random()*256);
                var b = Math.floor(Math.random()*256);
                 
                if(r < 16){//当小于16时,只有一位数,所以给它前面串联一个0
                    r = "0" r.toString(16);
                }else{
                    r = r.toString(16);
                }
                if(g < 16){
                    g = "0" g.toString(16);
                }else{
                    g = g.toString(16);
                }
                if(b < 16){
                    b = "0" b.toString(16);
                }else{
                    b = b.toString(16);
                }
                 
                return "#" r g b;
            }
             
            /*
                进制转换
                Number.toString(2);
                转换成281016
                 
            */
             
            function test(){
                //console.log(new Number(15).toString(16));
            }
            //test();

更多内容请见原文,文章转载自:https://blog.csdn.net/weixin_44519496/article/details/118540962

0 人点赞