```
<style>
* {
margin: 0;
padding: 0;
}
#container {
margin-left: auto;
margin-right: auto;
/* overflow: hidden; */
position: relative;
}
img {
width: 230px;
padding: 5px;
}
.content {
box-shadow: 0 0 1px 1px rgba(51, 51, 51, 0.1);
float: left;
position: relative;
width: 240px;
}
</style>
```
```
<div id="container">
<div class="content">
<img src="images/01.jpg" alt="">
</div>
<div class="content">
<img src="images/02.jpg" alt="">
</div>
<div class="content">
<img src="images/03.jpg" alt="">
</div>
<div class="content">
<img src="images/04.jpg" alt="">
</div>
<div class="content">
<img src="images/05.jpg" alt="">
</div>
<div class="content">
<img src="images/06.jpg" alt="">
</div>
<div class="content">
<img src="images/07.jpg" alt="">
</div>
<div class="content">
<img src="images/08.jpg" alt="">
</div>
<div class="content">
<img src="images/09.jpg" alt="">
</div>
<div class="content">
<img src="images/10.jpg" alt="">
</div>
<div class="content">
<img src="images/11.jpg" alt="">
</div>
<div class="content">
<img src="images/12.jpg" alt="">
</div>
<div class="content">
<img src="images/07.jpg" alt="">
</div>
<div class="content">
<img src="images/08.jpg" alt="">
</div>
<div class="content">
<img src="images/09.jpg" alt="">
</div>
<div class="content">
<img src="images/10.jpg" alt="">
</div>
<div class="content">
<img src="images/11.jpg" alt="">
</div>
<div class="content">
<img src="images/12.jpg" alt="">
</div>
</div>
```
```
window.onload = function () {
var url = {
dataUrl: [{
src: "01.jpg"
}, {
src: "02.jpg"
}, {
src: "03.jpg"
}, {
src: "04.jpg"
}, {
src: "05.jpg"
}, {
src: "06.jpg"
}, {
src: "07.jpg"
}, {
src: "08.jpg"
}, {
src: "09.jpg"
}, {
src: "10.jpg"
}, {
src: "11.jpg"
}, {
src: "12.jpg"
}]
}
var container = document.getElementById("container");
var contents = document.getElementsByClassName("content");
var data = url.dataUrl;
place_img();
window.onscroll = function(){
if(sCondition()){
for(let i = 0 ;i <data.length;i++){
let img = document.createElement("img");
img.src = "images/" + data[i].src;
let div = document.createElement("div");
div.className = "content";
div.append(img);
container.append(div);
place_img();
}
}
}
function sCondition(){
var sh = document.documentElement.scrollTop;
var vh = document.documentElement.clientHeight;
var offsetTop = contents[contents.length-1].offsetTop+contents[contents.length-1].offsetHeight/2
return sh+vh>offsetTop?true:false;
}
function place_img() {
var ww = window.innerWidth;
var cw = contents[0].offsetWidth;
//一排放置几张图片
var num = Math.floor(ww / cw);
container.style.width = num * cw + "px";
// 2. 将第一排的height放入一个数组
var content_arr = Array.prototype.slice.call(contents);
var arr_height = [];
content_arr.forEach((element, index) => {
if (index < num) {
arr_height.push(element.offsetHeight)
}
else {
//3.剩余图片放置的位置
//绝对定位, top = 数组中最小高度 ,left 为最小位置的 offsetLeft;
var minHeight = Math.min(...arr_height);
var minIndex = arr_height.indexOf(minHeight);
var offsetLeft = content_arr[minIndex].offsetLeft;
console.log(offsetLeft);
content_arr[index].style.position = "absolute";
content_arr[index].style.top = minHeight + "px";
content_arr[index].style.left = offsetLeft + "px";
// //4.数组高度改变,重新设置数组
arr_height[minIndex] += content_arr[index].offsetHeight;
}
})
}
}
```
- 效果实例
- 1.点击增加高度
- 2.tab页面切换
- 3. 列表切换
- 4. 隔行变色
- 5. swiper 轮播
- 6.vue
- 7.定时器
- 8. 向表格中添加数据
- 9 瀑布流
- 1.JavaScript基础
- 1. 变量
- 2. 调试
- 3.数据类型
- 4.转换
- 5.控制语句
- 6.运算
- 7. this
- 8 JSON对象和javascript对象的相互转换
- 2.JavaScript的控制语句
- 1. 基本控制语句
- 2.节点
- 2.1DOM补充
- 3. 函数
- js的模块化如何解决
- 不知道有什么用的
- 4.数组
- 5. String
- 补充
- 6.Ajax
- 1. 原生Ajax
- 2. HTTP/get/post
- 3.jQuery-Ajax
- 4.跨域
- 5.axios
- 6.封装
- Ajax效果
- ajax补充
- 7. 正则
- 1.创建正则表达式
- 2. 正则的api
- 3.正则语法
- 4.例子
- 量词
- 8.面向对象
- 1.原型
- ES6
- 模块化
- 1.回调地狱
- 什么是回调地狱
- 简单封装
- promise解决回调地狱
- generator解决回调地狱
- async解决回调地狱
- 2.封装
- Ajax,promise
- JavaScript难点
- 1. 闭包/作用域
- 2.原型链
- 3. 兼容性
- 适配
- JavaScript小效果
- 字符串截取