```
<style>
*{
margin: 0;
padding: 0;
}
img{
width: 230px;
padding: 5px;
vertical-align: bottom;
}
.content{
width: 240px;
border: 1px solid #eee;
/* box-shadow: 0 0 1px 1px #ccc; */
float: left;
position: relative;
}
#container{
position: relative;
margin-right: auto;
margin-left: auto;
}
</style>
</head>
<body>
<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>
<script>
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"
}]
}
window.onload = function(){
//确保内容加载完毕 否则无法获取高度
var container = document.getElementById("container");
var contents = document.getElementsByClassName("content");
var data = url.dataUrl;
palceImg();
function palceImg(){
var ww = window .innerWidth;
var cw = contents[0].offsetWidth;
var num = Math.floor(ww/cw);
container.style.width = num*cw+"px";
//取到图片整张数
var content_arr = Array.prototype.slice.call(contents);
//将contents变更为数组
var arr_height = [];
content_arr.forEach(function(element,index){
if(index<num){
arr_height.push(element.offsetHeight);
//也可将element替换成content_arr[index]
}
else{
var minHeight = Math.min(...arr_height);
console.log(minHeight);
var minIndex = arr_height.indexOf(minHeight);
console.log(minIndex);
var offsetLeft = content_arr[minIndex].offsetLeft;
element.style.position = "absolute";
element.style.top = minHeight+"px";
element.style.left = offsetLeft+"px";
arr_height[minIndex]+=content_arr[index].offsetHeight;
}
})
}
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);
palceImg();
}
}
}
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;
}
}
</script>
</body>
```
- 1.JS的基础知识
- (1)调试
- (2)变量
- (3)数据类型
- 数据类型之间的转换
- (4)全局变量和局部变量
- (5)运算符和表达式
- (6)数组
- 2.控制语句DOM,BOM,事件
- (1)控制语句
- (2)DOM的基础
- 节点
- 改变样式
- DOM事件
- 3.函数
- (1)声明函数
- (2)构造函数
- (3)函数的参数
- (4)函数的传参
- (5)改变this
- (6)重载
- (7)回调函数
- 4.数组
- (1)创建数组
- (2)增删改查
- (3)字符串与数组的转换
- 5.正则
- (1)创建正则
- (2)字符串中支持正则
- (3)语法
- 最核心的元字符
- 6.ajax
- (1)原生ajax
- (2)http,get,post
- (3)跨域
- (4)jQuery-ajax
- (5)axios
- 7.面向对象
- (1)原型
- (2)原型链,继承
- (3)多态
- 8.es6小结
- 9.js+canvas实现验证码
- 10.js的作用域
- 11.闭包
- 实例
- toggle
- 图片切换
- swiper
- 遮罩颜色渐变
- 表格添加
- 瀑布流
- ajax数据请求渲染
- 百度地图