滾屏加載更多數據,適合評論等頁面
本例的數據庫很簡單,一看就明了
復制代碼 代碼如下:
<div id="container">
<?php
$query=mysql_query("select * from content order by id desc limit 0,10");
while ($row=mysql_fetch_array($query)) {
?>
<div class="single_item">
<div class="element_head">
<div class="date"><?php echo date('m-d H:i',strtotime($row['updatetime']));?></div>
<div class="author"><?php echo $row['id'];?></div>
</div>
<div class="content"><?php echo $row['message'];?></div>
</div>
<?php } ?>
</div>
<div class="nodata"></div>
js文件
復制代碼 代碼如下:
<script type="text/javascript">
$(function(){
var winH = $(window).height(); //頁面可視區域高度
var i = 1;
$(window).scroll(function () {
var pageH = $(document.body).height();
var scrollT = $(window).scrollTop(); //滾動條top
var aa = (pageH-winH-scrollT)/winH;
if(aa<0.02){
$.getJSON("result.php",{page:i},function(json){
if(json){
var str = "";
$.each(json,function(index,array){
var str = "<div class=\"single_item\"><div class=\"element_head\">";
var str = str + "<div class=\"date\">"+array['date']+"</div>";
var str = str + "<div class=\"author\">"+array['author']+"</div>";
var str = str + "</div><div class=\"content\">"+array['content']+"</div></div>";
$("#container").append(str);
});
i++;
}else{
$(".nodata").show().html("別滾動了,已經到底了。。。");
return false;
}
});
}
});
});
</script>
result.php
復制代碼 代碼如下:
<?php
include("conn.php");
$page = intval($_GET['page']); //獲取請求的頁數
$start = $page*5;
$query=mysql_query("select * from content order by id desc limit $start,5");
while ($row=mysql_fetch_array($query)) {
$arr[] = array(
'content'=>$row['message'],
'author'=>$row['id'],
'date'=>date('m-d H:i',strtotime($row['updatetime']))
);
}
echo json_encode($arr); //轉換為json數據輸出
?>