1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
| <!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>瀑布流</title>
<style>
.pubuliu-box{
position: relative;
margin: 0 auto;
}
.pubuliu-box > .img-box{
float: left;
padding: 5px;
}
.pubuliu-box > .img-box > .img-inner{
border: 1px solid #cccccc;
padding: 5px;
border-radius: 5px;
-webkit-box-shadow: 0 0 5px #ccc;
-moz-box-shadow: 0 0 5px #ccc;
box-shadow: 0 0 5px #ccc;
}
.pubuliu-box > .img-box > .img-inner >img{
display: block;
width: 150px;
height: auto;
}
</style>
</head>
<body>
<div class="pubuliu-box">
<div class="img-box">
<div class="img-inner">
<img src="images/1.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/2.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/3.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/4.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/5.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/6.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/7.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/8.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/9.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/10.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/1.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/2.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/3.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/1.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/2.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/3.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/4.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/5.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/6.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/7.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/8.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/9.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/10.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/1.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/2.jpg" alt="picture"/>
</div>
</div>
<div class="img-box">
<div class="img-inner">
<img src="images/3.jpg" alt="picture"/>
</div>
</div>
</div>
<script>
/*单例模式*/
var addEvent=(function() {//返回兼容旧浏览器的事件监听
var instance;
function init() {
function temp(target,event,fn) {
if(window.addEventListener){
return target.addEventListener(event, fn);
}else if(window.attachEvent) {
return target.attachEvent('on'+event, fn);
}
}
return temp
}
return{
getInstance: function() {
if(!instance){
instance = init();
}
return instance;
}
}
}());
/*模块模式*/
function loadModule(moduleArr) {//页面初始化完成时载入模块
var event = addEvent.getInstance();
event(window,'load',function() {
moduleArr.forEach(function(el) {
el.init();
});
});
return moduleArr;
}
var pubuliuModule = (function() {
var parentEle = document.querySelector('.pubuliu-box');
var childEle = document.querySelectorAll('.img-box');
function init(){
//设置布局宽度
var docWidth = document.documentElement.offsetWidth || document.body.offsetWidth; //获取可视文档的宽度
var imgBoxWidth = childEle[0].offsetWidth;//获取图片盒子的宽度
var cols = Math.floor(docWidth / imgBoxWidth);//一行能放几张图片的个数
parentEle.style.width = cols * imgBoxWidth + 'px';//设置布局宽度
main(cols);
scrollAjax(cols);
}
//核心函数
function main(cols) {
//设置除了第一行图片,往后的图片的布局,计算出第一行图片当中的最小高度,然后把往后的图片用绝对定位摆放在其下边,依次类推。
var imgHeightArr =[],imgMinHeight, imgMinHeightIndex;
childEle = document.querySelectorAll('.img-box');
for(var i = 0,len = childEle.length; i < len; i++) {
if(i < cols) {
imgHeightArr[i] = childEle[i].offsetHeight;
}else{
imgMinHeight = Math.min.apply(null, imgHeightArr); //获取第一行图片的最小高度
imgMinHeightIndex = getImgMinIndex(imgHeightArr, imgMinHeight);//获取第一行图片最小高度对应的位置,即索引值
//设置接下来除了第一行图片的所有图片布局
childEle[i].style.position = 'absolute';
childEle[i].style.top = imgMinHeight + 'px';
childEle[i].style.left = childEle[imgMinHeightIndex].offsetLeft+'px';
imgHeightArr[imgMinHeightIndex] = imgMinHeight + childEle[i].offsetHeight;//重新设置最小高度
}
}
}
//获取第一行图片最小高度对应的位置,即索引值
function getImgMinIndex(imgHeightArr, imgMinHeight) {
for(var i in imgHeightArr) {
if(imgHeightArr[i] === imgMinHeight) {
return i;
}
}
}
function scrollAjax(cols) {
var ajaxData =[{src:'images/1.jpg'},{src:'images/2.jpg'},{src:'images/3.jpg'},{src:'images/4.jpg'},{src:'images/5.jpg'},{src:'images/6.jpg'},{src:'images/7.jpg'},{src:'images/8.jpg'},{src:'images/9.jpg'},{src:'images/10.jpg'}];
var event = addEvent.getInstance();
event(window,'scroll',function() {
if(scrollFlag()){
ajaxData.forEach(function(el) {
var imgBox = document.createElement('div');
var imgInner = document.createElement('div');
var img = document.createElement('img');
imgBox.className='img-box';
imgInner.className = 'img-inner';
img.src = el.src;
imgInner.appendChild(img);
imgBox.appendChild(imgInner);
parentEle.appendChild(imgBox);
});
main(cols);
}
})
}
//设置滚动条滚动到什么位置,加载图片
function scrollFlag() {
childEle = document.querySelectorAll('.img-box');
var lastImgOffsetTop = childEle[childEle.length - 1].offsetTop;
var lastImgHeight = childEle[childEle.length - 1].offsetHeight;
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;//获取滚动条向下滚滚去的高度
var docHeight = document.documentElement.clientHeight || document.body.clientHeight;//获取文档可视区域的高度
console.log(lastImgOffsetTop, scrollTop, docHeight,lastImgHeight);
if(lastImgOffsetTop < scrollTop+docHeight-lastImgHeight) {//滚动条拉动显示最后一张图片底部时加载
return true;
}else{
return false;
}
}
return {
init:init
}
}());
loadModule([pubuliuModule]);
</script>
</body>
</html>
|