1.html页面代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>瀑布流</title>
<script type="text/javascript" language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<link type="text/css" rel="stylesheet" href="css/waterfall.css" />
<script type="text/javascript" language="javascript" src="js/waterfall.js"></script>
</head>
<body>
<ul id="stage">
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>
2.js代码:
var tag = 0;
var t;
var pageHeight;
var top;
var height;
var flag = true;
$(document).ready(function(){
loadMore();
});
$(function(){
$(window).scroll(function(){
pageHeight = $(this).height();
height = $(document).height();
if((pageHeight+ (document.body.scrollTop || document.documentElement.scrollTop))>(height-150) && height !=0 && flag){
loadMore();
}
})
})
function loadMore()
{
tag++;
$.ajax({
type: "POST",
url : 'data.php',
dataType : 'json',
data: "tag="+tag,
success : function(json)
{
if(json.length!=0){
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 style="border:1px solid red;height:100px; margin-top:5px;">'+oProduct.id+'====='+oProduct.name+'</div>').hide();
$row.append($item);
$item.fadeIn();
}
}
}else{
flag =false;
}
}
});
}
php代码:
$conn = mysql_connect('localhost','root','') or die('数据库链接错误');
mysql_select_db('pubu',$conn);
$tag = $_POST['tag'];
$limit = ($tag-1)*10;
$sql = "select * from test limit $limit,10";
$query = mysql_query($sql);
$json = array();
while($row = mysql_fetch_assoc($query)){
$json[] = $row;
}
echo json_encode( $json );