var maxheight = 0;
for(var i = 0;i<$(".ceshi").length;i++){
if(maxheight <= $(".ceshi").eq(i).height()){
maxheight = $(".ceshi").eq(i).height();
}
}
$(".ceshi").css('height',parseInt(maxheight)+'px');
上面是JS代码,求指导该怎么去优化然后达到我想要的效果。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>瀑布流布局</title>
<style>
.waterfall {
column-count: 3;
column-gap: 0;
}
.item {
break-inside: avoid;
padding: 10px;
}
.item-content {
padding: 10px;
height: auto;
color: #686868;
box-sizing: border-box;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div class="waterfall">
<div class="item">
<div class="item-content">三月到大理赏樱花不远不近</div>
</div>
<div class="item">
<div class="item-content">三月到大理赏樱花不远不近,才是最好的距离余生,请带上自己的阳光回忆,在部队那些日子被遗忘的花儿春光</div>
</div>
<div class="item">
<div class="item-content">三月到大理赏樱花不远不近,才是最好的距离余生,请带上自己的阳光回忆</div>
</div>
<div class="item">
<div class="item-content">三月到大理赏樱花不远不近,才是最好的距离余生,请带上自己的阳光回忆</div>
</div>
<div class="item">
<div class="item-content">三月到大理赏樱花不远不近,才是最好的距离余生,请带上自己的阳光回忆</div>
</div>
<div class="item">
<div class="item-content">三月到大理赏樱花不远不近</div>
</div>
<div class="item">
<div class="item-content">三月到大理赏樱花不远不近,才是最好的距离余生,请带上自己的阳光回忆,在部队那些日子被遗忘的花儿春光</div>
</div>
<div class="item">
<div class="item-content">三月到大理赏樱花不远不近</div>
</div>
<div class="item">
<div class="item-content">三月到大理赏樱花不远不近,才是最好的距离余生,请带上自己的阳光回忆,在部队那些日子被遗忘的花儿春光</div>
</div>
</div>
</body>
</html>
不需要js代码也可以实现。
通过清除浮动,让父盒子被子盒子撑开就行
父盒子不设置高度,设置overflow: hidden;
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
.box{
width: 330px;
padding: 5px;
background-color: #9ff;
/* 清除浮动 */
overflow: hidden;
}
.son {
float: left;
margin: 5px;
width: 150px;
height: 100px;
background-color: #f0f;
}
</style>
</head>
<body>
<div class="box">
<div class="son"></div>
<div class="son"></div>
<div class="son"></div>
<div class="son"></div>
<div class="son"></div>
<div class="son"></div>
<div class="son"></div>
<div class="son"></div>
<div class="son"></div>
<div class="son"></div>
</div>
</body>
</html>
不需要设置高度,给个最小高度 min-height:220px; ,然后让其自适应高度即可
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.box {
vertical-align: top;
width: 50px;
display: inline-block;
border: 1px solid red;
}
</style>
</head>
<body>
<div class="box_father">
<div class="box" style="min-height: 100px">内容 内容 内容 内容 内容 内容 内容 内容 内容 内容 内容</div>
<div class="box" style="min-height: 100px"></div>
<div class="box" style="min-height: 100px"></div>
<div class="box" style="min-height: 100px"></div>
<div class="box" style="min-height: 100px"></div>
<div class="box" style="min-height: 100px"></div>
<div class="box" style="min-height: 100px"></div>
<div class="box" style="min-height: 100px"></div>
<div class="box" style="min-height: 100px"></div>
<div class="box" style="min-height: 100px"></div>
</div>
</body>
</html>
flexbox有个问题就是,它的瀑布流顺序是纵向开始的