首页 > 解决方案 > 在页面滚动上加载数据在移动浏览器中不起作用

问题描述

当我滚动页面时,我正在从数据库中获取数据。该代码在桌面浏览器上运行良好,但在移动浏览器上运行良好。

我从不同的帖子尝试了不同的解决方案,但都是徒劳的。

$(document).ready(function(){
 $('body').on('touchmove', onScroll);
 $(window).on('scroll', onScroll);
 function onScroll(){
  var lastID = $('.load-more').attr('lastID');
  if(($(window).scrollTop() + window.innerHeight >= document.body.scrollHeight) && (lastID != 0)){
     $.ajax({
        type:'POST',
        url:'getData.php',
        data:'Slno='+lastID,
        beforeSend:function(){
           $('.load-more').show();
        },
        success:function(html){
           $('.load-more').remove();
           $('#postList').append(html);
        }
    });
}
};
});

标签: phpjqueryajax

解决方案


试试这个代码:

$(document).ready(function(){
    $('body').on('touchmove', onScroll);
    $(window).on('scroll', onScroll);
    function onScroll(){
        var lastID = $('.load-more').attr('lastID');
        if ($(window).scrollTop() >  $(document).height() - $(window).height() - 100 && (lastID != 0)) {
            $.ajax({
                type:'POST',
                url:'getData.php',
                data:'Slno='+lastID,
                beforeSend:function(){
                    $('.load-more').show();
                },
                success:function(html){
                    $('.load-more').remove();
                    $('#postList').append(html);
                }
            });
        }
    };
});

推荐阅读