基于ajax實(shí)現(xiàn)點(diǎn)擊加載更多無(wú)刷新載入到本頁(yè)
先給大家展示效果圖:

本例是分頁(yè)的另外一種顯示方式,并不是隱藏未顯示的內(nèi)容
數(shù)據(jù)庫(kù)結(jié)構(gòu)與《ajax 翻頁(yè)》是一樣的
JavaScript 代碼
<script type="text/javascript">
$(document).ready(function() {
var track_click = ; //track user click on "load more" button, righ now it is click
var total_pages = <?php echo $total_pages; ?>;
$('#results').load("fetch_pages.php", {'page':track_click}, function() {track_click++;}); //initial data to load
$(".load_more").click(function (e) { //user clicks on button
$(this).hide(); //hide load more button on click
$('.animation_image').show(); //show loading image
if(track_click <= total_pages) //make sure user clicks are still less than total pages
{
//post page number and load returned data into result element
$.post('fetch_pages.php',{'page': track_click}, function(data) {
$(".load_more").show(); //bring back load more button
$("#results").append(data); //append data received from server
//scroll page to button element
$("html, body").animate({scrollTop: $("#load_more_button").offset().top}, );
//hide loading image
$('.animation_image').hide(); //hide loading image once data is received
track_click++; //user click increment on load button
}).fail(function(xhr, ajaxOptions, thrownError) {
alert(thrownError); //alert any HTTP error
$(".load_more").show(); //bring back load more button
$('.animation_image').hide(); //hide loading image once data is received
});
if(track_click >= total_pages-)
{
//reached end of the page yet? disable load button
$(".load_more").attr("disabled", "disabled");
}
}
});
});
</script>
XML/HTML代碼
<div id="results"></div> <div align="center"> <button class="load_more" id="load_more_button">load More</button> <div class="animation_image" style="display:none;"><img src="ajax-loader.gif"> Loading...</div> </div>
fetch_pages.php
php代碼
<?php
include("conn.php");
$item_per_page = 3;
//sanitize post value
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
//throw HTTP error if page number is not valid
if(!is_numeric($page_number)){
header('HTTP/1.1 500 Invalid page number!');
exit();
}
//get current starting point of records
$position = ($page_number * $item_per_page);
//Limit our results within a specified range.
$results = mysql_query("SELECT * FROM content ORDER BY id DESC LIMIT $position, $item_per_page");
//output results from database
echo '<ul class="page_result">';
while($row = mysql_fetch_array($results))
{
echo '<li id="item_'.$row["id"].'"><span class="page_name">'.$row["id"].') '.$row["name"].'</span><span class="page_message">'.$row["message"].'</span></li>';
}
echo '</ul>';
?>
以上內(nèi)容是小編給大家介紹的基于ajax實(shí)現(xiàn)點(diǎn)擊加載更多無(wú)刷新載入到本頁(yè),希望大家喜歡。
- 如何使用AJAX實(shí)現(xiàn)按需加載【推薦】
- ajax異步加載圖片實(shí)例分析
- JQuery實(shí)現(xiàn)Ajax加載圖片的方法
- js ajax加載時(shí)的進(jìn)度條代碼
- javascript+ajax實(shí)現(xiàn)產(chǎn)品頁(yè)面加載信息
- jQuery+AJAX實(shí)現(xiàn)無(wú)刷新下拉加載更多
- jQuery結(jié)合AJAX之在頁(yè)面滾動(dòng)時(shí)從服務(wù)器加載數(shù)據(jù)
- jQuery結(jié)合ajax實(shí)現(xiàn)動(dòng)態(tài)加載文本內(nèi)容
- Ajax加載外部頁(yè)面彈出層效果實(shí)現(xiàn)方法
- php+ajax+jquery實(shí)現(xiàn)點(diǎn)擊加載更多內(nèi)容
- php+ajax實(shí)現(xiàn)無(wú)刷新動(dòng)態(tài)加載數(shù)據(jù)技術(shù)
- jquery ajax局部加載方法詳解(實(shí)現(xiàn)代碼)
相關(guān)文章
Ajax實(shí)現(xiàn)列表無(wú)限加載和二級(jí)下拉選項(xiàng)效果
這篇文章主要為大家詳細(xì)介紹了Ajax實(shí)現(xiàn)列表無(wú)限加載和二級(jí)下拉選項(xiàng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
ASP小偷程序如何利用XMLHTTP實(shí)現(xiàn)表單的提交
ASP小偷程序如何利用XMLHTTP實(shí)現(xiàn)表單的提交...2006-07-07
ajax異步回調(diào)函數(shù)中給外部變量賦值的問(wèn)題探討
ajax異步回調(diào)函數(shù)中給外部變量賦值的問(wèn)題在本文將為大家詳細(xì)探討下,感興趣的朋友可以參考下2013-09-09
Ajax請(qǐng)求中async:false/true的作用分析
這篇文章主要介紹了Ajax請(qǐng)求中async:false/true的作用,結(jié)合實(shí)例形式分析說(shuō)明了Ajax請(qǐng)求中async:false/true的具體功能與使用技巧,需要的朋友可以參考下2016-04-04
Ajax獲取數(shù)據(jù)然后顯示在頁(yè)面的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇Ajax獲取數(shù)據(jù)然后顯示在頁(yè)面的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08
AJAX和三層架構(gòu)實(shí)現(xiàn)分頁(yè)功能具體思路及代碼
本文涉及到AJAX和三層架構(gòu)方面的知識(shí),在學(xué)習(xí)分頁(yè)的同時(shí)也鞏固了一下它們的相關(guān)知識(shí),適合初學(xué)者的你2013-05-05
使用AJAX返回WebService里的集合具體實(shí)現(xiàn)
如何使用AJAX返回WebService里的集合,在本文將有一個(gè)完美的實(shí)現(xiàn),感興趣的朋友可以參考下哈,希望可以幫助到你2013-05-05

