【发布时间】:2017-11-15 06:05:50
【问题描述】:
我有 jQuery 滚动自动加载更多。功能是使用PHP从数据库中加载更多数据。
该功能运行正常,当我尝试滚动时,将加载下一个数据。直到我遇到问题,当我将滚动条放到底部然后我刷新页面时,该功能已加载但它总是显示双数据。
到目前为止,这是我的 JS 函数:
$(document).ready(function()
{
$('#content').scrollPagination(
{
nop : 10,
id : 10,
offset : 0,
error : 'You have reached the end of the post.',
delay : 500,
scroll : true
});
});
(function($)
{
$.fn.scrollPagination = function(options)
{
var settings =
{
nop : 10, // The number of posts per scroll to be loaded
offset : 0, // Initial offset, begins at 0 in this case
error : 'No More Posts!',
delay : 500,
scroll : true
}
if(options)
{
$.extend(settings, options);
}
return this.each(function()
{
$this = $(this);
$settings = settings;
var offset = $settings.offset;
var busy = false;
if($settings.scroll == true) $initmessage = '';
else $initmessage = 'Click for more';
$this.append('<div class="content"></div><div class="loading-bar">'+$initmessage+'</div>');
$('.animation_images').show();
function getData()
{
$.post('load_post_user.php', {
action : 'scrollpagination',
number : $settings.nop,
offset : offset,
uid : '<?php echo $q_uid; ?>',
}, function(data) {
$this.find('.loading-bar').html($initmessage);
$('.animation_images').hide();
if(data == "") {
$this.find('.loading-bar').html($settings.error);
}
else {
offset = offset+$settings.nop;
$this.find('.content').append(data);
busy = false;
}
});
}
getData();
if($settings.scroll == true) {
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() > $this.height() && !busy) {
busy = true;
$('.animation_images').hide();
$this.find('.loading-bar').html('<img src="../assets/img/processing.gif"/>');
setTimeout(function() {
getData();
}, $settings.delay);
}
});
}
$this.find('.loading-bar').click(function() {
if(busy == false) {
busy = true;
getData();
}
});
});
}
})(jQuery);
【问题讨论】:
标签: javascript php jquery