溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么用php+jquery+html實(shí)現(xiàn)點(diǎn)擊不刷新加載效果

發(fā)布時間:2022-03-30 09:51:53 來源:億速云 閱讀:155 作者:iii 欄目:移動開發(fā)

今天小編給大家分享一下怎么用php+jquery+html實(shí)現(xiàn)點(diǎn)擊不刷新加載效果的相關(guān)知識點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

HTML

首先要引入jquery庫和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名字,但是在配置的時候一定要將對應(yīng)的class寫上。

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是定義加載動畫圖片的。

jQuery

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

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

PHP

data.php鏈接數(shù)據(jù)庫,本例使用本站文章PHP+Mysql+jQuery實(shí)現(xiàn)發(fā)布微博程序--PHP篇相同的數(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接收前臺頁面提交過來的兩個參數(shù),$_POST['last']即開始記錄數(shù),$_POST['amount']即單次顯示記錄數(shù),看SQL語句就明白,其實(shí)就是分頁中用到的語句。

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

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

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

以上就是“怎么用php+jquery+html實(shí)現(xiàn)點(diǎn)擊不刷新加載效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI