前言
今天记录一下,前端使用原生JS来实现瀑布流布局,其实在CSS3中新出了两个属性分别是:columns 和 column-gap 这两个值可以实现瀑布流布局,但是兼容性不强,而且也是别人已经写好的,感觉使用意义不大,接下来我们自己来实现一个瀑布流布局。
一:效果展示
二:HTML 部分
搭建基本结构,里面每一个div对应着一个图片。
<!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>Document</title>
<!-- 引入CSS文件 -->
<link rel="stylesheet" href="css/index.css" />
</head>
<body>
<div class="wrapper J_waterfall">
<div class="wf-Item"><img src="./img/1.jpg" alt="" class="wf-img"></div>
<div class="wf-Item"><img src="./img/2.jpg" alt="" class="wf-img"></div>
<div class="wf-Item"><img src="./img/3.jpg" alt="" class="wf-img"></div>
<div class="wf-Item"><img src="./img/4.jpg" alt="" class="wf-img"></div>
<div class="wf-Item"><img src="./img/5.jpg" alt="" class="wf-img"></div>
<div class="wf-Item"><img src="./img/6.jpg" alt="" class="wf-img"></div>
<div class="wf-Item"><img src="./img/7.jpg" alt="" class="wf-img"></div>
<div class="wf-Item"><img src="./img/8.jpg" alt="" class="wf-img"></div>
<div class="wf-Item"><img src="./img/9.jpg" alt="" class="wf-img"></div>
<div class="wf-Item"><img src="./img/10.jpg" alt="" class="wf-img"></div>
<div class="wf-Item"><img src="./img/11.jpg" alt="" class="wf-img">