欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php+ajax+jquery實(shí)現(xiàn)點(diǎn)擊加載更多內(nèi)容

 更新時(shí)間:2015年05月03日 15:53:58   投稿:hebedich  
本文給大家詳細(xì)講解的是如何利用jquery.more.js實(shí)現(xiàn)點(diǎn)擊加載更多后在本頁(yè)面內(nèi)下面加載數(shù)據(jù),十分的實(shí)用,有需要的小伙伴可以參考下。

我們?cè)谝恍┪⒉┚W(wǎng)站上可以碰到這樣的應(yīng)用,微博內(nèi)容列表沒(méi)有使用分頁(yè)條,而是一次加載一定數(shù)量的記錄顯示在列表頁(yè),當(dāng)用戶瀏覽到列表頁(yè)底部時(shí),可以通過(guò)單擊“查看更多”來(lái)加載更多記錄。本文我將結(jié)合jQuery和PHP給大家講述如何實(shí)現(xiàn)這種應(yīng)用。

基本原理:頁(yè)面載入時(shí),jQuery向后臺(tái)請(qǐng)求數(shù)據(jù),PHP通過(guò)查詢數(shù)據(jù)庫(kù)將最新的幾條記錄顯示在列表頁(yè),在列表頁(yè)的底部有個(gè)“更多”鏈接,通過(guò)觸發(fā)該鏈接,向服務(wù)端發(fā)送Ajax請(qǐng)求,后臺(tái)PHP程序得到請(qǐng)求參數(shù),并作出相應(yīng),獲取數(shù)據(jù)庫(kù)相應(yīng)的記錄并以JSON的形式返回給前臺(tái)頁(yè)面,前臺(tái)頁(yè)面jQuery解析JSON數(shù)據(jù),并將數(shù)據(jù)追加到列表頁(yè)。其實(shí)就是Ajax分頁(yè)效果。

首先要引入jquery庫(kù)和jquery.more.js插件,jquery.more.js已經(jīng)將許多功能都封裝好了,并提供了參數(shù)配置的功能。

<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript" src="jquery.more.js"></script> 

xhtml結(jié)構(gòu)如下:

<div id="more"> 
   <div class="single_item"> 
      <div class="element_head"> 
        <div class="date"></div> 
        <div class="author"></div> 
      </div> 
      <div class="content"></div> 
   </div> 
   <a href="javascript:;" class="get_more">::點(diǎn)擊加載更多內(nèi)容::</a> 
</div> 

值得一提的是,樣式single_item,get_more是和jquery.more.js插件關(guān)聯(lián)的,你也可以取另外的class名字,但是在配置的時(shí)候一定要將對(duì)應(yīng)的class寫(xiě)上。

CSS

#more{margin:10px auto;width: 560px; border: 1px solid #999;}        
.single_item{padding: 20px; border-bottom: 1px dotted #d3d3d3;} 
.author{position: absolute; left: 0px; font-weight:bold; color:#39f} 
.date{position: absolute; right: 0px; color:#999} 
.content{line-height:20px; word-break: break-all;} 
.element_head{width: 100%; position: relative; height: 20px;} 
.get_more{margin:10px; text-align:center} 
.more_loader_spinner{width:20px; height:20px; margin:10px auto; background: url(loader.gif) 
 no-repeat;} 

以上CSS是本例中定制的,當(dāng)然,大家可以在實(shí)際項(xiàng)目中定制不同的樣式。注意,more_loader_spinner是定義加載動(dòng)畫(huà)圖片的。

jQuery

$(function(){ 
  $('#more').more({'address': 'data.php'}) 
}); 

使用很簡(jiǎn)單,配置了后臺(tái)地址:data.php,來(lái)看data.php是怎么處理數(shù)據(jù)的。

PHP

data.php鏈接數(shù)據(jù)庫(kù),本例使用本站文章相同的數(shù)據(jù)表。

require_once('connect.php'); 
 
$last = $_POST['last']; 
$amount = $_POST['amount']; 
 
$user = array('demo1','demo2','demo3','demo3','demo4'); 
$query=mysql_query("select * from say order by id desc limit $last,$amount"); 
while ($row=mysql_fetch_array($query)) { 
  $sayList[] = array( 
    'content'=>$row['content'], 
    'author'=>$user[$row['userid']], 
    'date'=>date('m-d H:i',$row['addtime']) 
   ); 
} 
echo json_encode($sayList); 

data.php接收前臺(tái)頁(yè)面提交過(guò)來(lái)的兩個(gè)參數(shù),$_POST['last']即開(kāi)始記錄數(shù),$_POST['amount']即單次顯示記錄數(shù),看SQL語(yǔ)句就明白,其實(shí)就是分頁(yè)中用到的語(yǔ)句。

然后將查詢的結(jié)果以JSON格式輸出,PHP的任務(wù)就完成了。

最后來(lái)看下jquery.more.js的參數(shù)配置。

'amount'      :   '10',           //每次顯示記錄數(shù)
'address'     :   'comments.php', //請(qǐng)求后臺(tái)的地址
'format'      :   'json',         //數(shù)據(jù)傳輸格式
'template'    :   '.single_item', //html記錄DIV的class屬性
'trigger'     :   '.get_more',    //觸發(fā)加載更多記錄的class屬性
'scroll'      :   'false',        //是否支持滾動(dòng)觸發(fā)加載
'offset'      :   '100',          //滾動(dòng)觸發(fā)加載時(shí)的偏移量

以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。

相關(guān)文章

最新評(píng)論