平时自己练习时,经常会加点颜色以突显效果,我觉得很麻烦,而且自己能记住的颜色并不多,而且也不好选择,所以就自制了如下生成随机颜色的JS代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>两栏三栏自适应布局</title>
<style type="text/css">
/* 两栏三栏自适应布局 */
/* 两栏布局 主栏左 侧栏右 */
.g-mn1{float:left;width:100%;margin-right:-200px;}
.g-mnc1{margin-right:210px;}
.g-sd1{float:right;width:200px;}
/* 两栏布局 主栏右 侧栏左*/
.g-mn2{float:right;width:100%;margin-left:-200px;}
.g-mnc2{margin-left:210px;}
.g-sd2{float:left;width:200px;}
/* 三栏布局 主栏右 两侧栏左*/
.g-mn3{float:right;width:100%;margin-left:-520px;}
.g-mnc3{margin-left:520px;}
.g-sd3a{float:left;width:300px;margin-right:10px;}
.g-sd3b{float:left;width:200px;}
/* 三栏布局 主栏中 两侧栏分居左右*/
.g-mn4{float:right;width:100%;margin-left:-200px;}
.g-mnc4{margin-left:210px;}
.g-sd4{float:left;width:200px;}
.g-mn5{float:left;width:100%;margin-right:-200px;}
.g-mnc5{margin-right:210px;}
.g-sd5{float:right;width:200px;}
</style>
</head>
<body>
<div class="g-bd">
<div class="g-mn1">
<div class="g-mnc1">
<p>主栏1内容区</p>
</div>
</div>
<div class="g-sd1">
<p>侧栏1内容区</p>
</div>
</div>
<div class="g-bd">
<div class="g-mn2">
<div class="g-mnc2">
<p>主栏2内容区</p>
</div>
</div>
<div class="g-sd2">
<p>侧栏2内容区</p>
</div>
</div>
<div class="g-bd">
<div class="g-mn4">
<div class="g-mnc4">
<div class="g-mn5">
<div class="g-mnc5">
<p>主栏5内容区</p>
</div>
</div>
<div class="g-sd5">
<p>侧栏5内容区</p>
</div>
</div>
</div>
<div class="g-sd4">
<p>侧栏4内容区</p>
</div>
</div>
<div class="g-bd">
<div class="g-mn3">
<div class="g-mnc3">
<p>主栏3内容区</p>
</div>
</div>
<div class="g-sd3a">
<p>侧栏a内容区</p>
</div>
<div class="g-sd3b">
<p>侧栏b内容区</p>
</div>
</div>
</body>
<script>
function productColor(ele){
for(i=0; i<ele.length; i++){
var Addition = [];
for(var k=0; k<3; k++){
for(j=0; j<2000; j++){
if(Addition.length == 3){
break;
}
var rdm1 = Math.random().toFixed(1) * 10;
var rdm2 = Math.random().toFixed(1) * 10;
var rdm3 = Math.random().toFixed(1) * 10;
var Addit = Number(rdm1.toString() + rdm2.toString() + rdm3.toString());
if(Addit > 0 && Addit < 255){
Addition.push(Addit);
}else{
continue;
}
}
}
ele[i].style.backgroundColor = "rgb(" + Addition.toString() + ")";
}
}
productColor(document.getElementsByTagName("div")); /*生成颜色函数,参数为需要生成颜色的元素数组*/
</script>
</html>