<style type="text/css">
*{
padding: 0;
margin: 0;
}
.tubiao,.jihua,.shiji,.riqi{
width: 100%;
overflow: hidden;
margin-top: 10px;
}
.left{
width: 10%;
float: left;
text-align: center;
height: 25px;
line-height: 25px;
}
.right{
width: 90%;
float: right;
height: 25px;
}
span {
width: 5%;
height: 100%;
text-align: center;
display: inline-block;
}
</style>
<body>
<div class="tubiao">
<div class="jihua">
<div class="left">計(jì)劃</div>
<!--計(jì)劃span存放的地方-->
<div class="right plan"></div>
</div>
<div class="shiji">
<div class="left">實(shí)際</div>
<!--實(shí)際span存放的地方-->
<div class="right act"></div>
</div>
<div class="riqi" id="day_id">
<!--日期存放的地方-->
<div class="right day"></div>
</div>
</div>
<script type="text/javascript">
var temp1="0-0.5-2-2-2-2-1-1";//計(jì)劃耗時(shí)(塊的單位寬度)
var temp2="1-1-2-1-2-0-0-0";//實(shí)際耗時(shí)(塊的單位寬度)
var temp3="5/19-5/20-5/21-5/22-5/23-5/24-5/25-5/26-5/27-5/28";//綜合日期
var temp=temp1+"~"+temp2+"~"+temp3;
var plan = document.getElementsByClassName("plan")[0];
var act = document.getElementsByClassName("act")[0];
var day = document.getElementsByClassName("day")[0];
var num = 20;//創(chuàng)建多少個(gè)格
load_first(temp);
//分割數(shù)據(jù)和添加色塊操作
function load_first(temp){
var demo=temp.split("~");
var d1=demo[0].split("-");//計(jì)劃耗時(shí)(塊的單位寬度)數(shù)組
var d2=demo[1].split("-");////實(shí)際耗時(shí)(塊的單位寬度)數(shù)組
var d3=demo[2].split("-");//綜合日期數(shù)組
for(var i=0;i<d3.length;i++){
time_span(d3[i]);
}
//alert("6:"+d1.length+"---"+"3:"+d2.length);
//alert("d3.length:"+d3.length);
for(var i=0;i<d1.length;i++){
add_span(d1[i],d2[i],i);
}
document.getElementById("day_id").style.marginLeft="-30px";
}
//新增顏色塊,a為計(jì)劃顏色塊寬度,b為實(shí)際顏色塊寬度
function add_span(a,b,i){
//創(chuàng)建span塊
var span1 = document.createElement("span");
var span2 = document.createElement("span");
//定義隨機(jī)底色
var spa = "rgba(" + rnd(0,255)+ "," + rnd(0,255)+ ","+ rnd(0,255)+ ","+ rnd(0.5,1) +")";//每一個(gè)顏色隨機(jī)出來
if(i==0){
span1.style.backgroundColor = "000000";
//clientWidth是對(duì)象看到的寬度(不含邊線,即border)
span1.style.width = (plan.clientWidth/num*a) + "px";//計(jì)劃的每一格的寬度
//插入節(jié)點(diǎn)span1至plan
plan.appendChild(span1);
span2.style.backgroundColor = "000000";
span2.style.width = (plan.clientWidth/num*b) + "px";//實(shí)際的每一格的寬度
act.appendChild(span2);
}else{
//alert("a:"+a+"b:"+b+"i:"+i);
if(a=="0"){
span1.style.backgroundColor = "000000";
span1.style.width = (plan.clientWidth/num*a) + "px";//計(jì)劃的每一格的寬度
//插入節(jié)點(diǎn)span1至plan
plan.appendChild(span1);
}else{
span1.style.backgroundColor = spa;
//clientWidth是對(duì)象看到的寬度(不含邊線,即border)
span1.style.width = (plan.clientWidth/num*a) + "px";//計(jì)劃的每一格的寬度
//插入節(jié)點(diǎn)span1至plan
plan.appendChild(span1);
}
if(b=="0"){
span2.style.backgroundColor = "000000";
span2.style.width = (plan.clientWidth/num*b) + "px";//實(shí)際的每一格的寬度
act.appendChild(span2);
}else{
span2.style.backgroundColor = spa;
span2.style.width = (plan.clientWidth/num*b) + "px";//實(shí)際的每一格的寬度
act.appendChild(span2);
}
}
}
//日期的數(shù)據(jù)插入
function time_span(time){
//創(chuàng)建span塊
var span = document.createElement("span");
span.style.width = (plan.clientWidth/num*1) + "px";//每一個(gè)span的寬度
span.innerHTML = "" + time;
day.appendChild(span);
}
//隨機(jī)函數(shù)
function rnd(min,max){
return Math.round(Math.random()*(max - min)+min);
}
function QueryData() {
var displayStyle = "1";
$.ajax({
type: "post",
url: "Test.aspx",
dataType: "text",
data: { "DispalyStyle": displayStyle },
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown + XMLHttpRequest.responseText);
},
success: function (json) {
try {
load_first(json);
}
catch (e) { }
}
});
}
//QueryData();
</script>
</body>
以上所述是小編給大家介紹的Html 實(shí)現(xiàn)動(dòng)態(tài)顯示顏色塊的報(bào)表效果(實(shí)例代碼),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!