JavaScript输出各色系随机颜色【红绿蓝青黄紫、黑白、全彩】

JavaScript输出各色系随机颜色【红绿蓝青黄紫、黑白、全彩】

<!DOCTYPE html>
<html >
 <head>
  <title> 随机色系-黑白红绿蓝青黄紫</title>
  <meta charset="utf-8" />
  <script src="/js/jquery-2.1.4.min.js" type="text/javascript"></script>
  
 <style type="text/css">
	li {
		width: 50px;
		height: 50px;
		margin: 10px;
		display: inline-block;
		border-radius: 100%;
		background: rgba(1,1,1,1);
	}

	a {
		display: inline-block;
		width: 50px;
		height: 20px;
		margin: 10px;
		background: #ddd;
		text-align: center;
		border-radius: 6px;
	}
 </style>
<script type="text/javascript">
 $(function(){
	for (i=0;i<100 ;i++ ){
		$("li:first").clone().insertAfter("ul");
	}
	listColor(1,1,1,1);

});
//--------------------------
function listColor(r,g,b,a){
	$("li:first").css("background","rgba("+r*255+","+g*255+","+b*255+",1)");
	$("li:not(:first)").each(function(){
		$(this).css("background",getColor_rgba(r,g,b,a));
	});
}
//------------------------
function getColor_rgba(r, g, b, a) {//主函数:输出rgba颜色格式:"rgba(255,255,255,0.8)"
    var rgb = 155;
    var c = Math.floor(Math.random() * (255 - rgb) + rgb);
    if (r * g * b == 1) {
        r = Math.floor(Math.random() * 255);
        g = Math.floor(Math.random() * 255);
        b = Math.floor(Math.random() * 255);
    } else if (r + g + b == 0) {
        var t = Math.floor(Math.random() * 255);
        r = t;
        g = t;
        b = t;
    } else {
        r = r == 1 ? (Math.floor(Math.random() * (255 - rgb) + rgb)) : (Math.floor(Math.random() * (c / 2)));
        g = g == 1 ? Math.floor(Math.random() * (255 - rgb) + rgb) : Math.floor(Math.random() * (c / 2));
        b = b == 1 ? Math.floor(Math.random() * (255 - rgb) + rgb) : Math.floor(Math.random() * (c / 2));
    }
    return "rgba(" + r + "," + g + "," + b + "," + a + ")";
}
</script>
 </head>

 <body>
	<a href="javascript:listColor(1,0,0,0.8)">红</a>
	<a href="javascript:listColor(0,1,0,0.8)">绿</a>
	<a href="javascript:listColor(0,0,1,0.8)">蓝</a>
	<a href="javascript:listColor(0,1,1,0.8)">青</a>
	<a href="javascript:listColor(1,1,0,0.8)">黄</a>
	<a href="javascript:listColor(1,0,1,0.8)">紫</a>
	<a href="javascript:listColor(1,1,1,0.8)">全彩</a>
	<a href="javascript:listColor(0,0,0,0.8)">黑白</a>
	<ul><li></li></ul>
 </body>
</html>

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值