<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.box{
width: 200px;
background-color: aqua;
font-size: 100px;
text-align: center;
color: white;
position: absolute;
}
</style>
</head>
<body>
</body>
<script type="text/javascript">
//数组用来保存每一列的总高度
var heightArray=[0,0,0]
for(var i=0;i<40;i++){
var newDiv=document.createElement('div');
newDiv.className='box';
var num =Math.random()*100+50;
newDiv.style.height=num+"px";
//1、找出高度最小的那一列下标
var minIndex=0;
for(var j=1;j<heightArray.length;j++){
if(heightArray[minIndex]>heightArray[j]){
minIndex=j;
}
}
//2、设置Div的left=列下标*(200+10)
newDiv.style.left=minIndex*210+"px";
//3、设置div的top=最小列的高度+10;
newDiv.style.top=heightArray[minIndex]+"px";
//4、把Div的高度加到最小列
newDiv.innerHTML=i;
heightArray[minIndex]+=(num+10);
document.body.appendChild(newDiv);
}
</script>
</html>
网友评论