手写简单瀑布流

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
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title></title>
<style type='text/css'>
* {
margin: 0;
padding: 0;
}

img {
width: 180px;
}

.container {
margin: 0 auto;
position: relative;
background: #f8f8f8;
padding: 10px;
}

.box {
position: absolute;
}

.box>div {
float: left;
padding: 10px;
border: 1px solid #ccc;
box-shadow: 2px 2px 3px 0 #ccc;
border-radius: 10px;
margin: 10px;
}
</style>
</head>

<body>
<div class='container' id='container'></div>
</body>
<script src='http://libs.baidu.com/jquery/2.0.0/jquery.min.js'></script>
<script type='text/javascript'>
var imgs = [
'http://temp.im/180x100/f00',
'http://temp.im/180x200/ff0',
'http://temp.im/180x300/f0f',
'http://temp.im/180x400/00f',
'http://temp.im/180x200/0ff',
'http://temp.im/180x300/0f0',
'http://temp.im/180x100/f66',
'http://temp.im/180x600/66f',
'http://temp.im/180x200/636',
'http://temp.im/180x300/ef3',
'http://temp.im/180x100/f00',
'http://temp.im/180x200/ff0',
'http://temp.im/180x300/f0f',
'http://temp.im/180x400/00f',
'http://temp.im/180x200/0ff',
'http://temp.im/180x300/0f0',
'http://temp.im/180x100/f66',
'http://temp.im/180x600/66f',
'http://temp.im/180x200/636',
'http://temp.im/180x300/ef3',
'http://temp.im/180x100/f00',
'http://temp.im/180x200/ff0',
'http://temp.im/180x300/f0f',
'http://temp.im/180x400/00f',
'http://temp.im/180x200/0ff',
'http://temp.im/180x300/0f0',
'http://temp.im/180x100/f66',
'http://temp.im/180x600/66f',
'http://temp.im/180x200/636',
'http://temp.im/180x300/ef3'
];
var $con = $('#container');
var lefts = []; //保存每组的left
var tops = []; //保存高度
var imgID = 0; //图片id
var cols; //组数
var timer;

$.each(imgs, function() {
var $img = $('<div class='box'><div><img src='' + this + '' /></div></div>')
$img.appendTo($con);
setTimeout(function() {
waterFall(222)
}, 200)
});

$(window).resize(function() {
setTimeout(function() {
waterFall(222)
}, 200)
})

function waterFall(imgWidth) {
lefts = [];
tops = [];
var viewWidth = document.documentElement.clientWidth || window.innerWidth
cols = Math.floor(viewWidth / imgWidth);
$con.width(cols * imgWidth)
for(var i = 0; i < cols; i++) {
lefts.push(imgWidth * i);
tops.push(0);
};
//console.log(lefts)

$('.box').each(function() {
//console.log(getTop($(this).outerHeight()))
var pos = getPos($(this).outerHeight());
$con.height(Math.max.apply(null, tops));
$con.height();
$(this).css({
left: pos.left,
top: pos.top
})
})
}

function getPos(height) {
var pos = {};

var minTop = Math.min.apply(null, tops);
var minIndex = getIndex(minTop);
tops[minIndex] = minTop + height;
//console.log(minTop,tops)
pos.top = minTop;

pos.left = lefts[minIndex];
//console.log(pos)
return pos;
}

//获取tops最小下标
function getIndex(m) {
for(var i in tops) {
if(tops[i] == m) {
//console.log(i)
return i
}
}
}
</script>

</html>