JS 获取随机颜色值

function fn1(){
    return '#' + Math.floor( Math.random() * 0xffffff ).toString(16);
}


function fn2(){
    return '#' + ( Math.random() * 0xffffff<<0 ).toString(16);
}


function fn3(){
    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 fn4(){
    var color = "#";
    for( var i = 0; i < 6; i++ ){
        color += ( Math.random()*16 | 0 ).toString(16);
    }
    return color;
}


function fn5(){
    var colorValue = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f";
    var colorArray = colorValue.split(",");
    var color = "#";
    for( var i = 0; i < 6; i++ ){
        color += colorArray[ Math.floor( Math.random() * 16 ) ];
    }
    return color;
}
posted @ 2020-01-19 14:47  一心二念  阅读(605)  评论(0编辑  收藏  举报