无休止地在网页上滚动

时间:2016-03-25 12:48:51

标签: javascript php ajax laravel-5.2

我有一个无休止的页面滚动问题。发生了从下一页读取的新数据,但它会附加到页面的顶部,而不是它应该是。 请帮忙解决问题!

控制器:

RewriteEngine on 
RewriteCond %{THE_REQUEST} ^mydomain.com/mypage
RewriteRule ^(.*) http://app-heroku.com [P]

这是脚本:

public function index(Request $request)
{
    $posts = Post::paginate($this->posts_per_page); 

    if ($request->ajax()){
        return [
            'posts' => view('ajax.index', compact('posts'))->render(),
            'next_page' => $posts->nextPageUrl()
        ];
    }

    return view('index', compact('posts'));
}

UPD: 这是页面代码

$(document).ready(function(){
    $(window).scroll(fetchPosts);
    function fetchPosts() {
    var page = $('.endless-pagination').data('next-page');

    if (page !== null) {
        clearTimeout( $.data( this, "scrollCheck" ));

        $.data( this, "scrollCheck", setTimeout(function() {
            var scroll_position_for_posts_load = $(window).height() + $(window).scrollTop() + 100;

            if(scroll_position_for_posts_load >= $(document).height()){
                $.get(page, function(data){
                    $('.endless-pagination').data('next-page', data.next_page);
                    $('.posts').append(data.posts);
                });
            }
        }, 350))
    }
}
});

0 个答案:

没有答案