如何用ajax做无限滚动?
问题描述:
我使用ASP.NET。如何用ajax做无限滚动?
我想在我的页面上显示我的数据库文章和评论。我可以用一个函数显示他们。但我想添加无限滚动属性。当用户滚动页面时,页面应该重新加载并继续。你知道我喜欢Facebook,Twitter。
我该怎么做?这是我的代码在页面上显示数据。我在JQuery中搜索scroll
函数,但我没有一起做这件事。
$(function() {
var posts = jQuery.parseJSON($("#MainContent_hdnPosts").val());
$("#Post").html("");
var html = "";
$.each(posts, function (index, a) {
html += '<tr><td>' + a.userId + '</td></tr>';
});
$("#Post").append(html);
});
答
使用下面的代码我给PHP比如你做后端的ASP.NET:
负载
$(window).scroll(function() {
if ($(window).scrollTop() + $(window).height() >= $(document).height()) {
var last_id = $("#ticketHistoryData a:last-child").attr('id');
loadMoreData(last_id);
}
});
**
保护负载更多的数据
function loadMoreData(last_id) {
$.ajax(
{
url: '../../controllers/support/fetch_hdata.php?last_id=' + last_id ,
type: "get",
beforeSend: function() {
$('.ajax-load').show();
}
})
.done(function (data) {
$('.ajax-load').hide();
$("#ticketHistoryData").append(data);
})
.fail(function (jqXHR, ajaxOptions, thrownError) {
console.log('server not responding...');
});
}
在控制器收到ID和选择数据大于当前接收到的ID
select * from data where id < '" . $lastId . "' ORDER BY id DESC LIMIT 10;