js 随机色

颜色的赋值方式:
1.通过英文名
2.通过rgb三原色调配比例,赋值大于0,小于255;
在这里插入图片描述
3.通过16进制来匹配(一般不用)
透明度opacity
1为不透明,0为完全透明;
随机色

	<body>		
		<div class="box"></div>
		<div style="width: 100px; height: 100px; background: red;"></div>
		<script type="text/javascript">		
			var box = document.getElementsByClassName("box")[0]; //获取元素	
			function c(){
				return  Math.floor(Math.random()*255);//通过随机数获取到一个数值
			}	
			//通过使用 rgb三原色 来完成随机颜色的赋值 
			box.style.backgroundColor = 'rgb(' + c() + ',' + c() + ','+ c()+ ')';
		</script>
	</body>
发布了41 篇原创文章 · 获赞 3 · 访问量 4670

猜你喜欢

转载自blog.csdn.net/weixin_46146313/article/details/104115844