写了一个简单的瀑布流布局
效果是这样的
下面是代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>原生瀑布流</title>
<style>
* {
padding: 0;
margin: 0;
}
body{padding-top: 30px;}
.box {
position: relative;
width: 750px;
margin: 0 auto;
background: pink;
}
.box .time {
position: absolute;
background: #ff00ff;
width: 45%;
}
.box .time img {
width: 100%;
}
//奇数偶数所在的位置统一下
.box .time:nth-child(odd) {
left: 3.5%;
}
.box .time:nth-child(even) {
left: 51.5%;
}
</style>
</head>
<body>
<div class="box">
<div class="time" style="top:0px">
<img src="https://www.17sucai.com/preview/1/2017-08-21/pull/finishing/img/works2.jpg" alt="">
<div>
<h4>标题</h4>
<h3>标题</h3>
</div>
</div>
<div class="time ">
<img src="https://www.17sucai.com/preview/1/2017-08-21/pull/finishing/img/works4.jpg" alt="">
<h4>标题</h4>
<h3>标题</h3>
</div>
<div class="time">
<div>
<img src="https://www.17sucai.com/preview/1/2017-08-21/pull/finishing/img/works6.jpg" alt="">
<h4>标题</h4>
<h3>标题</h3>
</div>
</div>
<div class="time ">
<img src="https://www.17sucai.com/preview/1/2017-08-21/pull/finishing/img/works4.jpg" alt="">
<h4>标题</h4>
<h3>标题</h3>
</div>
<div class="time ">
<img src="https://www.17sucai.com/preview/1/2017-08-21/pull/finishing/img/works4.jpg" alt="">
<h4>标题</h4>
<h3>标题</h3>
</div>
<div class="time ">
<img src="https://www.17sucai.com/preview/1/2017-08-21/pull/finishing/img/works4.jpg" alt="">
<h4>标题</h4>
<h3>标题</h3>
</div>
<div class="time">
<div>
<img src="https://www.17sucai.com/preview/1/2017-08-21/pull/finishing/img/works6.jpg" alt="">
<h4>标题</h4>
<h3>标题</h3>
</div>
</div>
<div class="time">
<img src="https://www.17sucai.com/preview/1/2017-08-21/pull/finishing/img/works2.jpg" alt="">
<div>
<h4>标题</h4>
<h3>标题</h3>
</div>
</div>
</div>
<script>
window.onload = function () {
function potion() {
var item = document.getElementsByClassName('time')
for (let index = 0; index < item.length; index++) {
console.log(item[index].offsetHeight)
// 默认开始两条不计算
if (index > 1) {
// 判断上面的元素高度加距离顶部的距离 外加15px的边距
let top = item[index - 2].offsetTop + (item[index - 2].offsetHeight) + 15
item[index].setAttribute('style', 'top:' + top + 'px;')
} else {
item[index].setAttribute('style', 'top:0px;')
}
}
}
// 默认有数据
potion()
// 数据参数
var imgarr = ['https://www.17sucai.com/preview/1/2017-08-21/pull/finishing/img/works2.jpg', 'https://www.17sucai.com/preview/1/2017-08-21/pull/finishing/img/works6.jpg', 'https://www.17sucai.com/preview/1/2017-08-21/pull/finishing/img/works4.jpg','https://www.17sucai.com/preview/1/2017-08-21/pull/finishing/img/works2.jpg', 'https://www.17sucai.com/preview/1/2017-08-21/pull/finishing/img/works6.jpg', 'https://www.17sucai.com/preview/1/2017-08-21/pull/finishing/img/works4.jpg']
window.onscroll = function () {
if (getScrollTop() + getClientHeight() === getScrollHeight()) {
console.log('上拉加载了')
// 上拉触底加载模拟请求成功反回数据
var item = document.getElementsByClassName('time')
var box = document.getElementsByClassName('box')[0]
for (let index = 0; index < imgarr.length; index++) {
//给要插入的元素设置内容
var insertDiv = document.createElement("div");
insertDiv.className = "time";
var insertDiv1 = document.createElement("div");
var img = document.createElement('img');
var lineP1 = document.createElement('h3');
var lineP2 = document.createElement('h4');
img.src = imgarr[index]
lineP1.innerText = '标题'
lineP2.innerText = '标题'
insertDiv.appendChild(insertDiv1)
insertDiv1.appendChild(img)
insertDiv1.appendChild(lineP1)
insertDiv1.appendChild(lineP2)
box.appendChild(insertDiv)
}
// 执行适配位置
potion()
}
}
// 上拉触底
function getScrollTop() {
let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
return scrollTop
}
function getClientHeight() {
let clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
return clientHeight;
}
function getScrollHeight() {
let scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight
return scrollHeight
}
}
</script>
</body>
</html>
主要思路是绝对定位,根据上一个domo的高度和顶部距离去决定他所在的位置的,小白一个多多包涵