JS 随机颜色

2020-09-18  本文已影响0人  Bytesking

方法一

     function getRandomColor(){
      return  '#'+Math.random().toString(16).slice(2,8)
  }

方法二

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

方法三

    function getRandomColor(){
        return '#'+Math.floor(Math.random()*256).toString(10);
    }

方法四

    let getRandomColor = function(){    
        return  '#' + (function(color){    
             return (color +=  '0123456789abcdef'[Math.floor(Math.random()*16)])    
             && (color.length == 6) ?  color : arguments.callee(color);    
        })('');    
     } 

方法五

  function getRandomColor(){
     this.r = Math.floor(Math.random()*255);
     this.g = Math.floor(Math.random()*255);
     this.b = Math.floor(Math.random()*255);
     this.color = 'rgba('+ this.r +','+ this.g +','+ this.b +',0.8)';
  }
上一篇下一篇

猜你喜欢

热点阅读