js随机产生不同颜色

迈不过友情╰ 2022-05-18 07:14 543阅读 0赞

方法一(随机RGB颜色值)#####

  1. //颜色对象
  2. function getRandomColor(){
  3. this.r = Math.floor(Math.random()*255);
  4. this.g = Math.floor(Math.random()*255);
  5. this.b = Math.floor(Math.random()*255);
  6. this.color = 'rgba('+ this.r +','+ this.g +','+ this.b +',0.8)';
  7. }

方法二 (生成十六进制的颜色值)

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

方法三

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

方法四

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

方法五

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

发表评论

表情:
评论列表 (有 0 条评论,543人围观)

还没有评论,来说两句吧...

相关阅读

    相关 随机颜色

    随机颜色 开发工具与关键技术:JavaScript 作者:陈希雄 撰写时间:2019/2/19 提醒:其实也可以拼接成自己想要的图形或者文字 操