天天看點

Jquery滑鼠滾動到頁面底部自動加載更多内容,使用分頁 PHP

  1. index.php代碼  

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3.     <head>  
  4.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5.         <title>滾屏加載--無重新整理動态加載資料技術的應用-www.corange.cn</title>  
  6.         <style type="text/css">  
  7.             #container{margin:10px auto;width: 660px; border: 1px solid #999;}   
  8.             .single_item{padding: 20px; border-bottom: 1px dotted #d3d3d3;}  
  9.             .author{position: absolute; left: 0px; font-weight:bold; color:#39f}  
  10.             .date{position: absolute; right: 0px; color:#999}  
  11.             .content{line-height:20px; word-break: break-all;}  
  12.             .element_head{width: 100%; position: relative; height: 20px;}  
  13.             .nodata{display:none; height:32px; line-height:32px; text-align:center; color:#999; font-size:14px}  
  14.         </style>  
  15.         <script type="text/javascript" src="../jquery.js"></script>  
  16.         <script type="text/javascript">  
  17.             $(function() {  
  18.                 var winH = $(window).height(); //頁面可視區域高度  
  19.                 var i = 1;  
  20.                 $(window).scroll(function() {  
  21.                     var pageH = $(document.body).height();  
  22.                     var scrollT = $(window).scrollTop(); //滾動條top  
  23.                     var aa = (pageH - winH - scrollT) / winH;  
  24.                     if (aa < 0.02) {  
  25.                         $.getJSON("result.php", {page: i}, function(json) {  
  26.                             if (json) {  
  27.                                 var str = "";  
  28.                                 $.each(json, function(index, array) {  
  29.                                     var str = "<div class=\"single_item\"><div class=\"element_head\">";  
  30.                                     var str = str + "<div class=\"date\">" + array['date'] + "</div>";  
  31.                                     var str = str + "<div class=\"author\">" + array['author'] + "</div>";  
  32.                                     var str = str + "</div><div class=\"content\">" + array['content'] + "</div></div>";  
  33.                                     $("#container").append(str);  
  34.                                 });  
  35.                                 i++;  
  36.                             } else {  
  37.                                 $(".nodata").show().html("别滾動了,已經到底了。。。");  
  38.                                 return false;  
  39.                             }  
  40.                         });  
  41.                     }  
  42.                 });  
  43.             });  
  44.         </script>  
  45.     </head>  
  46.     <?php   
  47.     require_once('connect.php');   
  48.     $user = array('demo1','demo2','demo3','demo3','demo4');   
  49.     ?>   
  50.     <div id="container">   
  51.         <?php   
  52.         $query=mysql_query("select * from comments order by id desc limit 0,15");   
  53.         while ($row=mysql_fetch_array($query)) {   
  54.         ?>   
  55.         <div class="single_item">   
  56.             <div class="element_head">   
  57.                 <div class="date"><?php echo date('m-d H:i',$row['addtime']);?></div>   
  58.                 <div class="author"><?php echo $user[$row['userid']];?></div>   
  59.             </div>   
  60.             <div class="content"><?php echo $row['content'];?></div>   
  61.         </div>   
  62.         <?php } ?>   
  63.     </div>   
  64.     <div class="nodata"></div>   

result.PHP代碼

  1. <?php  
  2. require_once('connect.php'); //連接配接資料庫   
  3. $user = array('demo1','demo2','demo3','demo3','demo4');   
  4. $page = intval($_GET['page']);  //擷取請求的頁數   
  5. $start = $page*15;   
  6. $query=mysql_query("select * from comments order by id desc limit $start,15");   
  7. while ($row=mysql_fetch_array($query)) {   
  8.     $arr[] = array(   
  9.         'content'=>$row['content'],   
  10.         'author'=>$user[$row['userid']],   
  11.         'date'=>date('m-d H:i',$row['addtime'])   
  12.     );   
  13. }   
  14. echo json_encode($arr);  //轉換為json資料輸出   
  15. ?>  

connect.php代碼

  1. $host="localhost";  
  2. $db_user="root";  
  3. $db_pass="";  
  4. $db_name="demo";  
  5. $timezone="Asia/Shanghai";  
  6. $link=mysql_connect($host,$db_user,$db_pass);  
  7. mysql_select_db($db_name,$link);  
  8. mysql_query("SET names UTF8");  
上一篇: js
下一篇: JQ