相信很多人都见过瀑布流图片布局,那些图片是动态加载出来的,效果很好,对服务器的压力相对来说也小了很多,用鼠标操作的时候相信都见过这样的效果:进入qq空间,向下拉动空间,到底部时,会动态加载剩余的说说或者是日志 ,今天我们就来看看他们的实现思路和js控制动态加载的代码。
window.onscroll =
function
() {
var
scrollTop = document.body.scrollTop;
var
offsetHeight = document.body.offsetHeight;
var
scrollHeight = document.body.scrollHeight;
if
(scrollTop == scrollHeight - offsetHeight) {
page++;
loadList(page);
}
};
function
loadList(page) {
page = page || 1;
if
(isLoad) {
getJSON(
'/forum.php?mod=hot&page='
+ page).then(
function
(data) {
if
(data.code == 200) {
data = data.data;
if
(data && Object.keys(data).length > 0) {
for
(
var
k
in
data) {
var
v = data[k];
detailTemplate = detailTemplate.cloneNode(
true
);
var
userInfoObj = detailTemplate.getElementsByClassName(
'user-info'
)[0];
userInfoObj.getElementsByClassName(
'name'
)[0].innerText = v.author;
userInfoObj.getElementsByClassName(
'avatar'
)[0].src = v.avatar;
userInfoObj.getElementsByClassName(
'post-time'
)[0].innerHTML = v.lastpost;
detailTemplate.getElementsByClassName(
'title'
)[0].innerText = v.subject;
detailTemplate.getElementsByClassName(
'desc'
)[0].innerText = v.subject;
var
extInfoObj = detailTemplate.getElementsByClassName(
'ext-info'
)[0];
extInfoObj.getElementsByClassName(
'from'
)[0].innerText = v.fname;
extInfoObj.getElementsByClassName(
'view-time'
)[0].innerText = v.views;
postListObj.appendChild(detailTemplate);
}
}
else
{
isLoad =
false
;
}
}
else
{
isLoad =
false
;
}
},
function
(status) {
console.log(
'Something went wrong, status is '
+ status);
});
}
}