/
1801.html
113 lines (105 loc) · 2.32 KB
/
1801.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>瀑布流布局</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
<style>
.ct {
position: relative;
}
.ct>ul>li {
position: absolute;
width: 200px;
margin-right: 10px;
margin-top: 10px;
transition: all 1s;
list-style: none;
}
.h1 {
height: 200px;
background-color: #f4b300;
}
.h2 {
height: 300px;
background-color: #691BB8;
}
.h3 {
height: 400px;
background-color: #006ac1;
}
.h4 {
height: 500px;
background-color: #aabbcc;
}
</style>
</head>
<body>
<div class="ct">
<ul>
<li class="h1">1</li>
<li class="h2">2</li>
<li class="h3">3</li>
<li class="h4">4</li>
<li class="h2">5</li>
<li class="h4">6</li>
<li class="h3">7</li>
<li class="h1">8</li>
<li class="h1">9</li>
<li class="h2">10</li>
<li class="h3">11</li>
<li class="h1">12</li>
<li class="h1">13</li>
<li class="h2">14</li>
</ul>
</div>
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
var colHeightArr, boxWidth, colCount;
init();
layout();
//绑定事件,当窗口发生变化时重新计算
$(window).on('resize', function() {
init();
layout();
})
function init() {
colHeightArr = [];
boxWidth = $('.ct>ul>li').outerWidth(true);
colCount = parseInt($('.ct').width() / boxWidth);
//初始化,每一列高度之和
for(var i=0; i<colCount; i++) {
colHeightArr.push(0);
}
}
function layout() {
//水平方向位置:拿到下标
//垂直方向位置:最小高度
$('.ct>ul>li').each(function() {
var $box = $(this);
var minObject = getMin(colHeightArr),
minValue = minObject.minValue,
minIndex = minObject.minIndex;
$box.css({left: boxWidth * minIndex, top: minValue});
colHeightArr[minIndex] += $box.outerHeight(true);
})
}
//从数组中获取最小值并得到下标
function getMin(arr) {
if(arr.length === 0) return;
var value = arr[0],
index = 0;
for(var i=0; i<arr.length; i++) {
if(arr[i] < value) {
value = arr[i];
index = i;
}
}
return {minValue: value, minIndex: index};
}
</script>
</body>
</html>