先来写一个简单的图片页面
复制代码
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/main.css">
<script type="text/javascript" src="../lib/jquery/jquery-1.11.1.min.js"></script>
</head>
<body>
<p id="main">
<p class="box">
<p class="pic">
<img src="">
</p>
</p>
<p class="box">
<p class="pic">
<img src="">
</p>
</p>
<!-- 这里省略多个class为box的p-->
<p class="box">
<p class="pic">
<img src="">
</p>
</p>
</p>
</body>
<script type="text/javascript">
var width = 300, height = 300;
$('.box img').each(function(){
// 随机图片的高宽,如果大小一样,就没必要用瀑布流了
width = Math.floor(Math.random() * 100) + 300;
height = Math.floor(Math.random() * 500) + 300;
$(this).attr('src', 'https://placekitten.com/'+ height +'/' + width);
});
</script>
</html>
复制代码
假设,宽和高都是350,生成链接为https://placekitten.com/350/350,访问这个link就会得到一张350X350的可爱的小猫图片~~O(∩_∩)O~~
然后,添加相应的CSS即可
复制代码
* {
padding: 0;
margin: 0;
}
#main {
-webkit-column-count: 4;
-moz-column-count: 4;
column-count: 4;
-moz-column-gap:20px;
-webkit-column-gap:20px;
column-gap:20px;
padding: 10px;
}
.box {
padding: 15px;
border: solid 2px #eeeeee;
border-radius: 4px;
margin-bottom: 15px;
cursor: pointer;
}
.box img {
width: 100%;
}