<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>瀑布流</title>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
#main{
position: relative;
}
.box{
padding: 15px 0 0 15px;
float: left;
}
.img{
padding: 10px;
border: 1px solid gray;
border-radius: 10px;
box-shadow: 0px 0px 5px #ccc;
}
.img img{
width: 120px;
height: auto;
}
</style>
</head>
<body>
<div id="main">
<div class="box">
<div class="img"><img src="img/1.jpg" alt=""></div>
</div>
<div class="box">
<div class="img"><img src="img/2.jpg" alt=""></div>
</div>
<div class="box">
<div class="img"><img src="img/3.jpg" alt=""></div>
</div>
<div class="box">
<div class="img"><img src="img/4.jpg" alt=""></div>
</div>
<div class="box">
<div class="img"><img src="img/5.jpg" alt=""></div>
</div>
<div class="box">
<div class="img"><img src="img/6.jpg" alt=""></div>
</div>
<div class="box">
<div class="img"><img src="img/7.jpg" alt=""></div>
</div>
<div class="box">
<div class="img"><img src="img/8.jpg" alt=""></div>
</div>
<div class="box">
<div class="img"><img src="img/9.jpg" alt=""></div>
</div>
<div class="box">
<div class="img"><img src="img/10.jpg" alt=""></div>
</div>
<div class="box">
<div class="img"><img src="img/11.jpg" alt=""></div>
</div>
<div class="box">
<div class="img"><img src="img/12.jpg" alt=""></div>
</div>
<div class="box">
<div class="img"><img src="img/13.jpg" alt=""></div>
</div>
<div class="box">
<div class="img"><img src="img/14.jpg" alt=""></div>
</div>
<div class="box">
<div class="img"><img src="img/15.jpg" alt=""></div>
</div>
<div class="box">
<div class="img"><img src="img/16.jpg" alt=""></div>
</div>
<div class="box">
<div class="img"><img src="img/17.jpg" alt=""></div>
</div>
<div class="box">
<div class="img"><img src="img/18.jpg" alt=""></div>
</div>
<div class="box">
<div class="img"><img src="img/19.jpg" alt=""></div>
</div>
<div class="box">
<div class="img"><img src="img/20.jpg" alt=""></div>
</div>
<div class="box">
<div class="img"><img src="img/21.jpg" alt=""></div>
</div>
</div>
<script type="text/javascript">
var wrap=document.getElementById('main');
var boxImg=document.getElementsByClassName('box');
//图片盒子的宽度
var boxImgWidth=boxImg[0].offsetWidth;
//网页宽度
var clientWdith=document.documentElement.clientWidth;
//图片列数
var imgCol=Math.floor(clientWdith/boxImgWidth);
wrap.style.width=boxImgWidth*imgCol+'px';
wrap.style.margin='0 auto';
//存放每一列的高度
var hArr=[];
for (let i = 0; i < boxImg.length; i++) {
if (i<imgCol) {
console.log(boxImg[i].offsetHeight);
hArr.push(boxImg[i].offsetHeight);
}else{
//找出高度最低的box;
var minH=Math.min.apply(null,hArr);
var index=getMinIndex(minH,hArr);
console.log(index,minH);
// boxImg[i].style.position='absolute';
// boxImg[i].style.left=index*boxImgWidth+'px';
// boxImg[i].style.top=minH+'px';
}
}
console.log(hArr);
function getMinIndex(value,arr) {
for (let i = arr.length - 1; i >= 0; i--) {
if (arr[i]==value) {
return i;
}
}
}
</script>
</body>
</html>
还有clientWidth 是系统保留字啊, 你最好别用
还有你的机器是混杂模式?
你确定你写的是JavaScript?