向下滚动时无限滚动不起作用

问题描述 投票:0回答:1

我有一个代码无限滚动,但是我有一个问题。为什么我的代码在向上滚动而不在向下滚动时起作用?

<script type="text/javascript">
$(document).ready(function(){
    $(window).scroll(function(){
        var lastID = $('.load-more').attr('lastID');
        if(($(window).scrollTop() == $(document).height() - $(window).height()) && (lastID != 0)){
     //Why work when scroll up? i want this work when scroll down
            $.ajax({
                type:'POST',
                url:'<?php echo base_url('berita/load_more'); ?>',
                data:'id='+lastID,
                beforeSend:function(){
                    $('.load-more').show();
                },
                success:function(html){
                    $('.load-more').remove();
                    $('#postList').append(html);
                }
            });
        }
    });
});
</script>

https://tabungamal.id/berita/infinite上观看实时演示

javascript infinite-scroll
1个回答
0
投票

您可以更改来源。

<script type="text/javascript">
$(document).ready(function(){
    $(window).scroll(function(){
        var lastID = $('.load-more').attr('lastID');
//Try modifying here
        if(((window.innerHeight + window.scrollY) >= document.body.offsetHeight) && (lastID != 0)){

            $.ajax({
                type:'POST',
                url:'<?php echo base_url('berita/load_more'); ?>',
                data:'id='+lastID,
                beforeSend:function(){
                    $('.load-more').show();
                },
                success:function(html){
                    $('.load-more').remove();
                    $('#postList').append(html);
                }
            });
        }
    });
});
</script>

© www.soinside.com 2019 - 2024. All rights reserved.