【问题标题】:Infinite scroll : Sequencing post with Ajax and php无限滚动:使用 Ajax 和 php 对帖子进行排序
【发布时间】:2017-09-02 16:43:55
【问题描述】:

我正在尝试使用 ajax php 和 jquery 无限滚动我的页面。 我已经粘贴了 jquery 代码 doe 参考。我正在尝试使用 jquery 和 php 按顺序发布。我的 jquery 代码将 counta 发送到 php 脚本。 php 脚本然后根据它收到的数字返回结果。我正在尝试按顺序发布帖子,以免它们重复。当用户到达页面底部时,我会收到帖子。

也欢迎任何其他答案。但我只想使用jquery的核心含义,不包含任何外部库。

var counta = 0;
$(document).ready(function(){


    $(window).scroll(function() {

   if($(window).scrollTop() + $(window).height() > $(document).height() - 2000) {            
    //  console.log("Here");

    $.ajax({            
                            type: 'post',
                            url: 'extendost.php',               
                            data: '&articles='+counta,              
                            success: function (data) {                                  
                            alert(counta);
                            $("#morepost").append(data);                                                    
                            counta++;
                            },
                            error: function (data) {                            
                            alert(data);                                
                            },
          });

   }
}); 

【问题讨论】:

    标签: php jquery ajax infinite-scroll


    【解决方案1】:

    这就是我会做的:

    <div id="start">0</div>
    <div class="post_content"></div>
    
    $(window).data('ajaxready', true).scroll(function(e) {
        var postHeight = $('#post_content').height();
        if ($(window).data('ajaxready') == false) return;
            var start = parseInt($('#start').text());
            var limit = 30; // return 30 posts each
            if ($(window).scrollTop() >= postHeight - $(window).height() - 400) {
                var height = $(window).scrollTop();
                $(window).data('ajaxready', false);
                if (start > 0) {
                    var new_start = start + limit;
                    var dataString = {articles : start, limit : limit};
                    $.ajax({
                        cache: false,
                        url: 'extendost.php',
                        type: 'POST',
                        data: dataString,
                        success: function(data) {
                            if ($.trim(data).indexOf('NO POST') > -1) {
                                /* stop infinite scroll when no more data */
                                $('#start').text(0);
                            } else {
                                $('#post_content').append(data);
                                $('#start').text(new_start);
                            }
                            $(window).data('ajaxready', true);
                        }
                    });
                }
            }
        });
    

    这样,您的#start 值将在滚动时发生变化,并为下一个滚动值做好准备

    然后根据 $start (offset) 和 $limit 进行查询

    $query = "SELECT * FROM articles LIMIT {$limit} OFFSET {$start}";
    if (!$result = mysqli_query($con, $query) {
      echo 'NO POST';
    } else {
      // fetch and echo your result;
    }
    

    【讨论】:

    • 我对您的回答进行了编辑。它需要 .html 代替 .val 因为我们需要获取 div 标签之间的 html 而不是它们的值。
    • 是的,我已经更新了我的答案。我将其更改为 .text() 而不是 .val()。否则,必须将 #start 更改为 input type="hidden"。谢谢
    猜你喜欢
    • 2019-11-04
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    • 2010-10-19
    相关资源
    最近更新 更多