
如何在 jquery 中点击按钮弹窗,并使用 ajax 异步加载不同分类 id 的数据,并在每个选项卡滚动到页底后进行翻页?
对于这个问题,解决方案是:
具体代码实现如下:
// 初始化分类 ID、当前页码和总页数
let categoryId = 1, currentPage = 1, total = 0;
// 是否加载中
let isLoading = false;
$(document).on('click', '.btn', function() {
loadCategoryData(categoryId, currentPage);
});
$('.tab_p p').click(function() {
currentPage = 1;
categoryId = $(this).data('id');
// 加载对应分类的数据
loadCategoryData(categoryId, currentPage);
});
function loadCategoryData(id, page) {
$(".tab_item").html('加载中...');
$(this).addClass('cur').siblings().removeClass('cur');
loadPageData(id, page);
}
// 监听滚动事件
$('.tab_item').scroll(function() {
console.log('scroll...', $('.tab_item').scrollTop(), $('.tab_item').innerHeight())
if (isLoading) {
return;
}
// 判断是否滚动到底部距离150px 加载更多
const scrollTop = $(this).scrollTop();
const scrollHeight = $(this).prop('scrollHeight');
const containerHeight = $(this).outerHeight();
if (scrollHeight - scrollTop - containerHeight < 150) {
// 滚动到底部距离小于150px,加载更多数据
currentPage++;
if (currentPage <= total) {
loadPageData(categoryId, currentPage);
}
}
});
function loadPageData(categoryId, page) {
if (isLoading) {
return;
}
isLoading = true;
$.ajax({
url: '/ajax.php?mod=tab',
method: 'POST',
data: {
type_id: categoryId,
page: page
},
success: function(response) {
var obj = JSON.parse(response);
var html = "";
for (var i = 0; i < obj.length; i++) {
html += '<div>@@##@@<span>"' + obj[i].title + '"</span></div>';
}
$(".tab_item").append(html);
}
}).always(function() {
isLoading = false;
});
}注意:
以上就是如何使用 jQuery 点击按钮弹窗,并通过 AJAX 异步加载不同分类 ID 的数据,并在每个选项卡滚动到底部时进行翻页?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号