美文网首页
JS 随机颜色

JS 随机颜色

作者: Bytesking | 来源:发表于2020-09-18 10:49 被阅读0次

    方法一

         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)';
      }
    

    相关文章

      网友评论

          本文标题:JS 随机颜色

          本文链接:https://www.haomeiwen.com/subject/qundyktx.html