-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
152 lines (144 loc) · 4.35 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>新闻页懒加载瀑布流布局</title>
<script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
main {
position: relative;
width: 900px;
margin: 0 auto;
}
.single-news {
width: 280px;
margin: 10px;
padding: 10px;
border: 1px solid #ccc;
}
.news-preview {
width: 100%;
}
.news-message>dt {
margin: 10px 0 8px;
padding-bottom: 5px;
border-bottom: 1px solid #ccc;
font-weight: bold;
}
.news-message>dl {
font-size: 12px;
line-height: 1.8;
color: #777371;
}
</style>
</head>
<body>
<main></main>
<div class="loading"></div>
</body>
<script>
// 1.获取图片
// 2.瀑布流方式渲染
// 3.滚动到底部加载新的图片
var width = 300,
maxWidth = $('main').width(),
col = Math.floor(maxWidth / width);
clientHeightArr = [],
perPageCount = 10,
curPage = 1;
for(var i=0;i<col;i++) {
clientHeightArr.push(0);
}
getData();
$(window).scroll(throttle(function() {
if(isShow($('.loading'))) {
console.log('show');
getData();
}
}, 300));
function getData() {
$.ajax({
url: 'http://platform.sina.com.cn/slide/album_tech',
dataType: 'jsonp',
jsonp:"jsoncallback",
data: {
app_key: '1271687855',
num: perPageCount,
page: curPage
}
}).done(function(res){
if(res && res.status && res.status.code==='0') {
handleData(res.data);
curPage++;
} else {
console.log('数据获取失败');
}
})
}
function handleData(data) {
// 1.用数据生成节点
// 2.将节点用瀑布流的方式渲染上去
$.each(data, function(index, news) {
var $node = getNode(news);
$node.find('img').load(function() {
waterFallRender($node);
})
})
}
function getNode(data) {
var node = '\n' +
' <div class="single-news">\n' +
' <a href="' + data.url + '"><img class="news-preview" src="' + data.img_url + '"></a>\n' +
' <div class="news-message">\n' +
' <dt>' + data.short_name + '</dt>\n' +
' <dl>' + data.short_intro + '</dl>\n' +
' </div>\n' +
' </div>';
return $(node);
}
function waterFallRender($node) {
// 1.先计算列数
// 2.找到高度最小值的列数,放入
var minIndex = findMin(clientHeightArr);
$node.css({
position: 'absolute',
top: clientHeightArr[minIndex],
left: minIndex*width
});
$('main').append($node);
clientHeightArr[minIndex] += $node.outerHeight(true); // 记得加入之后才有高度
$('main').height(Math.max.apply(null, clientHeightArr)); // 没有高度需要手动设定高度
}
function findMin(arr) {
var minIndex = 0,
minValue = arr[0];
for(var i=1;i<arr.length;i++) {
if(minValue>arr[i]) {
minIndex = i;
minValue = arr[i];
}
}
return minIndex;
}
function isShow($node) {
return $node.offset().top <= $(window).height() + $(window).scrollTop();
}
function throttle(fn, delay) {
var isHandleFinished = true;
return function () {
if(isHandleFinished===false) {return}
var _this = this;
isHandleFinished = false;
setTimeout(function () {
fn.apply(_this, arguments);
isHandleFinished = true;
}, delay);
}
}
</script>
</html>