下拉滾動條或鼠標滾輪滾動到頁面底部時, 動態即時加載新內容。
后台用 json 傳輸數據, 示例程序中只寫了示例數組。數據也只設置了兩個屬性, 需根據實際應用改寫。
頁面用了 ul li 做為容器, 每個 li 表示一列
<ul id="stage">
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
JS代碼
/*
@版本日期: 版本日期: 2012年4月11日
@著作權所有: 1024 intelligence ( http://www.1024i.com )
獲得使用本類庫的許可, 您必須保留著作權聲明信息.
報告漏洞,意見或建議, 請聯系 Lou Barnes(iua1024@gmail.com)
*/
$(document).ready(function(){
loadMore();
});
$(window).scroll(function(){
// 當滾動到最底部以上100像素時, 加載新內容
if ($(document).height() - $(this).scrollTop() - $(this).height()<100) loadMore();
});
function loadMore()
{
$.ajax({
url : 'data.php',
dataType : 'json',
success : function(json)
{
if(typeof json == 'object')
{
var oProduct, $row, iHeight, iTempHeight;
for(var i=0, l=json.length; i<l; i++)
{
oProduct = json[i];
// 找出當前高度最小的列, 新內容添加到該列
iHeight = -1;
$('#stage li').each(function(){
iTempHeight = Number( $(this).height() );
if(iHeight==-1 || iHeight>iTempHeight)
{
iHeight = iTempHeight;
$row = $(this);
}
});
$item = $('<div><img src="'+oProduct.image+'" border="0" ><br />'+oProduct.title+'</div>').hide();
$row.append($item);
$item.fadeIn();
}
}
}
});
}
示例網址: http://product.1024i.com/ajax/waterfall/
資源下載: http://download.csdn.net/detail/iua1024/4216228
