Jquery鼠标滚动到页面底部自动加载更多内容,使用分页

1.index.php


  1.         <style type="text/css">  
  2.             #container{margin:10px auto;width: 660px; border: 1px solid #999;}   
  3.             .single_item{padding: 20px; border-bottom: 1px dotted #d3d3d3;}  
  4.             .author{position: absolute; left: 0px; font-weight:bold; color:#39f}  
  5.             .date{position: absolute; right: 0px; color:#999}  
  6.             .content{line-height:20px; word-break: break-all;}  
  7.             .element_head{width: 100%; position: relative; height: 20px;}  
  8.             .nodata{display:none; height:32px; line-height:32px; text-align:center; color:#999; font-size:14px}  
  9.         </style>  
  10.         <script type="text/javascript" src="../jquery.js"></script>  
  11.         <script type="text/javascript">  
  12.             $(function() {  
  13.                 var winH = $(window).height(); //页面可视区域高度  
  14.                 var i = 1;  
  15.                 $(window).scroll(function() {  
  16.                     var pageH = $(document.body).height();  
  17.                     var scrollT = $(window).scrollTop(); //滚动条top  
  18.                     var aa = (pageH - winH - scrollT) / winH;  
  19.                     if (aa < 0.02) {  
  20.                         $.getJSON("result.php", {page: i}, function(json) {  
  21.                             if (json) {  
  22.                                 var str = "";  
  23.                                 $.each(json, function(index, array) {  
  24.                                     var str = "<div class=\"single_item\"><div class=\"element_head\">";  
  25.                                     var str = str + "<div class=\"date\">" + array['date'] + "</div>";  
  26.                                     var str = str + "<div class=\"author\">" + array['author'] + "</div>";  
  27.                                     var str = str + "</div><div class=\"content\">" + array['content'] + "</div></div>";  
  28.                                     $("#container").append(str);  
  29.                                 });  
  30.                                 i++;  
  31.                             } else {  
  32.                                 $(".nodata").show().html("别滚动了,已经到底了。。。");  
  33.                                 return false;  
  34.                             }  
  35.                         });  
  36.                     }  
  37.                 });  
  38.             });  
  39.         </script>  
  40.  
  41.     <?php   
  42.     require_once('connect.php');   
  43.     $user = array('demo1','demo2','demo3','demo3','demo4');   
  44.     ?>   
  45.     <div id="container">   
  46.         <?php   
  47.         $query=mysql_query("select * from comments order by id desc limit 0,15");   
  48.         while ($row=mysql_fetch_array($query)) {   
  49.         ?>   
  50.         <div class="single_item">   
  51.             <div class="element_head">   
  52.                 <div class="date"><?php echo date('m-d H:i',$row['addtime']);?></div>   
  53.                 <div class="author"><?php echo $user[$row['userid']];?></div>   
  54.             </div>   
  55.             <div class="content"><?php echo $row['content'];?></div>   
  56.         </div>   
  57.         <?php } ?>   
  58.     </div>   
  59.     <div class="nodata"></div>   

result.php代码

  1. <?php  
  2. require_once('connect.php'); //连接数据库   
  3.    
  4. $user = array('demo1','demo2','demo3','demo3','demo4');   
  5. $page = intval($_GET['page']);  //获取请求的页数   
  6. $start = $page*15;   
  7. $query=mysql_query("select * from comments order by id desc limit $start,15");   
  8. while ($row=mysql_fetch_array($query)) {   
  9.     $arr[] = array(   
  10.         'content'=>$row['content'],   
  11.         'author'=>$user[$row['userid']],   
  12.         'date'=>date('m-d H:i',$row['addtime'])   
  13.     );   
  14. }   
  15. echo json_encode($arr);  //转换为json数据输出   
  16. ?>  


connect.php代码

  1. <?php  
  2. $host="localhost";  
  3. $db_user="root";  
  4. $db_pass="";  
  5. $db_name="demo";  
  6. $timezone="Asia/Shanghai";  
  7.   
  8. $link=mysql_connect($host,$db_user,$db_pass);  
  9. mysql_select_db($db_name,$link);  
  10. mysql_query("SET names UTF8");  
  11. ?> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值