0

我有一段 JS 代码,我只需将这些部分连接在一起即可使其工作。我的 js 代码将加载更多内容,从数据库中获取。直到现在它工作得很好,当用户单击链接时,将加载更多内容。

现在我想让它在页面接近尾声时自动加载。我的代码:

加载更多功能

function myFunction() {
    $(window).scroll(function () {
        if ($(window).scrollTop() >= $(document).height() - $(window).height() - 10) {
            $("load_more"); < ---- ** HOW DO I CALL THE LOAD FUNCTION ?**
    }
    });

    $(function () {
        $(document).on("click", ".load_more", function () {
            var last_msg_id = $(this).attr("id");
            var device =<? php echo $idevice; ?>;
            if (last_msg_id != 'end') {
                $.ajax({//Make the Ajax Request
                    type: "POST",
                    url: "index_more.php",
                    data: "lastmsg=" + last_msg_id + "&aa=" + device,
                    beforeSend: function () {
                        $('a.load_more').html('<img src="img/loading.gif" />');
                    },
                    success: function (html) {
                        $("#more").remove();
                        $("ol#updates").append(html);
                    }
                });

            }
            return false;
        });
    });

所以,我的问题是如何从 $(window).scroll 调用“load_more”函数?

谢谢!

4

1 回答 1

3

只需触发点击按钮,

$(window).scroll(function () {
   if ($(window).scrollTop() >= ($(document).height() - $(window).height() - 10)) {
      $(".load_more").trigger('click'); 
   }
});

所以还需要一些修改,

$(function() {

    $(window).scroll(function () {
       if ($(window).scrollTop() >= ($(document).height() - $(window).height() - 10)) {
          $(".load_more").trigger('click'); 
       }
    });

    $(document).on("click",".load_more",function() {
        var last_msg_id = $(this).attr("id");
        var device=<?php echo $idevice; ?>;
        if(last_msg_id!='end'){
            $.ajax({//Make the Ajax Request
                type: "POST",
                url: "index_more.php",
                data: "lastmsg="+ last_msg_id+"&aa="+device,
                beforeSend:  function() {
                    $('a.load_more').html('<img src="img/loading.gif" />');
                },
                success: function(html){
                    $("#more").remove();
                    $("ol#updates").append(html);
                }
            });

        }
        return false;
    });
});

希望这符合要求。

于 2013-09-27T07:57:31.793 回答