-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path瀑布流布局实战.html
248 lines (223 loc) · 8.37 KB
/
瀑布流布局实战.html
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
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>瀑布流之自探究</title>
<style>
* {
margin: 0;
padding: 0;
}
.main {
position: relative;
}
.box {
/* border: 1px dashed black;*/
float: left;
}
.pic {
padding: 10px 10px 10px 10px;
}
.pic img {
border: 1px solid gray;
width: 100%;
display: block;
background-size: cover;
border-radius: 5px;
box-shadow: 0px 0px 23px 4px blue;
}
.clearfix:after {
display: block;
content: '';
visibility: hidden;
line-height: 0;
height: 0;
clear: both;
}
.clearfix {
*zoom: 1;
}
</style>
</head>
<body>
<div class="main clearfix">
<div class="box">
<div class="pic"><img src="images/01.png" alt=""/></div>
</div>
<div class="box">
<div class="pic"><img src="images/02.png" alt=""/></div>
</div>
<div class="box">
<div class="pic"><img src="images/03.png" alt=""/></div>
</div>
<div class="box">
<div class="pic"><img src="images/04.png" alt=""/></div>
</div>
<div class="box">
<div class="pic"><img src="images/05.png" alt=""/></div>
</div>
<div class="box">
<div class="pic"><img src="images/06.png" alt=""/></div>
</div>
<div class="box">
<div class="pic"><img src="images/07.png" alt=""/></div>
</div>
<div class="box">
<div class="pic"><img src="images/08.png" alt=""/></div>
</div>
<div class="box">
<div class="pic"><img src="images/09.png" alt=""/></div>
</div>
<div class="box">
<div class="pic"><img src="images/10.png" alt=""/></div>
</div>
<div class="box">
<div class="pic"><img src="images/11.png" alt=""/></div>
</div>
<div class="box">
<div class="pic"><img src="images/12.png" alt=""/></div>
</div>
<div class="box">
<div class="pic"><img src="images/13.png" alt=""/></div>
</div>
<div class="box">
<div class="pic"><img src="images/14.png" alt=""/></div>
</div>
<div class="box">
<div class="pic"><img src="images/15.png" alt=""/></div>
</div>
<div class="box">
<div class="pic"><img src="images/16.png" alt=""/></div>
</div>
<div class="box">
<div class="pic"><img src="images/17.png" alt=""/></div>
</div>
<div class="box">
<div class="pic"><img src="images/18.png" alt=""/></div>
</div>
<div class="box">
<div class="pic"><img src="images/19.png" alt=""/></div>
</div>
<div class="box">
<div class="pic"><img src="images/20.png" alt=""/></div>
</div>
</div>
<script src="jquery-3.1.0.js"></script>
<script>
console.time("abc")
//load是字符串
$(window).on("load", function () {
waterfall();
/* checkscroll()*/
//模拟从后台加载图片
var json = {"data": [{'src': 'images/03.png'}, {'src': 'images/09.png'}, {'src': 'images/10.png'}, {'src': 'images/11.png'}, {'src': 'images/12.png'}, {'src': 'images/13.png'}, {'src': 'images/14.png'}, {'src': 'images/15.png'}]}
$(window).on("scroll", function () {
/*console.log(1);*/
if (checkscroll()) {
$.each(json.data, function (i, v) {
$('<div></div>').addClass('box').appendTo($('.main'));
$('<div></div>').addClass('pic').appendTo('.box:last');
$('<img>').appendTo('.pic:last').attr("src", v.src);
/*$('<img>').attr("src", v.src)*/
waterfall();
})
}
})
}).on("resize", function () {
console.log("感应窗口扩大缩小");
waterfall();
/* checkscroll()*/
//模拟从后台加载图片
var json = {"data": [{'src': 'images/03.png'}, {'src': 'images/09.png'}, {'src': 'images/10.png'}, {'src': 'images/11.png'}, {'src': 'images/12.png'}, {'src': 'images/13.png'}, {'src': 'images/14.png'}, {'src': 'images/15.png'}]}
$(window).on("scroll", function () {
/*console.log(1);*/
if (checkscroll()) {
console.log(1);
$.each(json.data, function (i, v) {
$('<div></div>').addClass('box').appendTo($('.main'));
$('<div></div>').addClass('pic').appendTo('.box:last');
$('<img>').appendTo('.pic:last').attr("src", v.src);
/*$('<img>').attr("src", v.src)*/
waterfall();
})
}
})
});
//以瀑布的形式排列图片
function waterfall() {
var w = $(".main>div").eq(0).width();
//把第一个子div的宽度设置给后面所有的div
$(".main>div").css("width", w);
//确定每一行放多少列
console.log($(window).width());
var n = Math.floor($(window).width() / w);
/*console.log(n);*/
$(".main").width(n * w);
$(".main").css("margin", "0 auto");
var narray = [];
var minnarray, minindex;
$(".main>div").each(function (index, element) {
//记录第一行每个div的高
if (index < n) {
/*narray.push($(element).outerHeight(true))*/
narray[index] = $(element).outerHeight();
//解决窗口缩小没问题 但是窗口扩大出现问题的情况 通过调试看代码可以发现这个问题
//若窗口变小,只放下3张图,第4,5,6张会被添加上else后的style,所以style还要改回来
$(element).css({
"position": "static",
"top": "0",
"left": "0"
/* "position":"",
"top":"",
"left":""*/
});
} else {
minnarray = Math.min.apply(null, narray)
//求出数组中最小高度的索引
minindex = $.inArray(minnarray, narray);
/* narray[index]=$(element).outerHeight();*/
//思考下面这个话与narray[minindex]+=$(element).height()为什么有冲突????
/* narray[minindex]+=narray[index]; 这句话是错误的数组的索引没有超过n*/
/*$(element).offset().width=minindex*w+"px";
$(element).offset().height=narray[index]+"px";*/
/*console.log($(element).offset());*/
//offset偏移始终是相对于浏览器左上角
//这样不能设置
/*$(element).offset().left=minindex*w+"px";
$(element).offset().top=narray[index]+"px";*/
//这样设置
/*$(element).offset({"left":minindex*w,"top":narray[index]})*/
/* $(element).css({
"position":"absolute",
"left":minindex*w,
"top":minnarray
})*/
//上面的设置方法也是可行的
$(element).css({
"position": "absolute",
"left": $(".main>div").eq(minindex).position().left,
"top": minnarray
})
narray[minindex] += $(element).height()
}
})
}
function checkscroll() {
/* console.log($(".main>div:last").position().top);
console.log($(".main>div:last").offset().top);
console.log($(window).width());
console.log(1);*/
/*console.log($(".main>div:last").position().top + $(".main>div:last").outerHeight());
console.log($(window).scrollTop() );
console.log($(window).height() );
console.log($(window).height()+$(window).scrollTop());
console.log($(".main>div:last").offset())
能正确打印的前提是先执行waterflall
*/
//判断最后一张图的中间位置与网页被蜷曲的长度和页面的高度之和的大小,来确定加不加载图片
return ($(".main>div:last").position().top + $(".main>div:last").outerHeight() / 2) < ($(window).height() + $(window).scrollTop()) ? true : false;
}
console.timeEnd("abc")
</script>
</body>
</html>