简体   繁体   English

Laravel使用Jquery和AJAX在滚动中加载更多数据

[英]Laravel load more data on scroll with Jquery and AJAX

This is my code here 这是我的代码在这里

<script>
  $(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_post_load = $(window).height() + $(window).scrollTop + 100;

          if(scroll_position_for_post_load >= $(document).height(){

            $.get(page, function(data){

              $('.posts').append(data.posts);


              $('.endless-pagination').data('next-page', data.next_page);

            });

          }

        }, 350))

      }

    }

  });
</script>

I replaced the IF condition 我替换了IF条件

scroll_position_for_post_load >= $(document)height()

with

1 == 1

This worked, but the page loads continuously without the user scrolling. 这可行,但是页面不断加载而无需用户滚动。

This is the code in my PagesController Variable page was declared at the top 这是我的PagesController Variable页面中的代码在顶部声明

public function index(Request $request){

    $posts = Post::orderBy('created_at', 'desc')->paginate($this->paginate_posts);

    if ($request->ajax()) {

        return [
            'posts' => view('pages.ajax.index')->with(compact('posts'))->render(),
            'next_page' => $posts->nextPageUrl()
        ];

    }

    return view('pages.index')->with(compact('posts'));

}

And on my index.blade.php page i have 在我的index.blade.php页面上

    @forelse ($posts as $post)

    {{--  Post and replies attached  --}}
    <div class="media stream">
        <a href="{{route('profile')}}" class="media-avatar medium pull-left">
            <img src="{{asset('images/user.png')}}">
        </a>
        <div class="media-body">
            <div class="stream-headline">
                <h5 class="stream-author">
                    <a href="http://localhost/blog/public/question/{{$post->slug}}" class="center">{{ $post->title }}</a>
                </h5>

                <h5 class="stream-author">
                    <a href="{{route('profile')}}" style="text-decoration:none;color:initial">{{ $post->user->name }}</a>
                    <small> {{ date('F d, Y', strtotime($post->created_at)) }} at {{ date('h:i A', strtotime($post->created_at)) }} </small>
                </h5>

                <div class="stream-text">
                    {{ $post->body }}
                </div>
            </div><!--/.stream-headline-->

            <div class="stream-options">
                <a href="#" class="btn btn-small">
                    <i class="icon-thumbs-up shaded"></i>
                    Like
                </a>
                <a href="http://localhost/blog/public/question/{{$post->slug}}" class="btn btn-small">
                    <i class="icon-reply shaded"></i>
                    Reply
                </a>
                <a href="#" class="btn btn-small">
                    <i class="icon-retweet shaded"></i>
                    Repost
                </a>
                <span class="stream"> {{$post->replies->count()}} comment (s) </span>
            </div>
        </div>{{--  media body  --}}
    </div><!--/.media .stream-->

@empty

    <div class="media stream">
        <div class="media-body">
            <div class="stream-headline">
                <div class="stream-text">
                    All clean. No posts found!!
                </div>
            </div><!--/.stream-headline-->
        </div>
    </div><!--/.media .stream-->

@endforelse

On pages.ajax.index i have 在pages.ajax.index我有

    @forelse ($posts as $post)

    {{--  Post and replies attached  --}}
    <div class="media stream">
        <a href="{{route('profile')}}" class="media-avatar medium pull-left">
            <img src="{{asset('images/user.png')}}">
        </a>
        <div class="media-body">
            <div class="stream-headline">
                <h5 class="stream-author">
                    <a href="http://localhost/blog/public/question/{{$post->slug}}" class="center">{{ $post->title }}</a>
                </h5>

                <h5 class="stream-author">
                    <a href="{{route('profile')}}" style="text-decoration:none;color:initial">{{ $post->user->name }}</a>
                    <small> {{ date('F d, Y', strtotime($post->created_at)) }} at {{ date('h:i A', strtotime($post->created_at)) }} </small>
                </h5>

                <div class="stream-text">
                    {{ $post->body }}
                </div>
            </div><!--/.stream-headline-->

            <div class="stream-options">
                <a href="#" class="btn btn-small">
                    <i class="icon-thumbs-up shaded"></i>
                    Like
                </a>
                <a href="http://localhost/blog/public/question/{{$post->slug}}" class="btn btn-small">
                    <i class="icon-reply shaded"></i>
                    Reply
                </a>
                <a href="#" class="btn btn-small">
                    <i class="icon-retweet shaded"></i>
                    Repost
                </a>
                <span class="stream"> {{$post->replies->count()}} comment (s) </span>
            </div>
        </div>{{--  media body  --}}
    </div><!--/.media .stream-->

@empty

    <div class="media stream">
        <div class="media-body">
            <div class="stream-headline">
                <div class="stream-text">
                    All clean. No posts found!!
                </div>
            </div><!--/.stream-headline-->
        </div>
    </div><!--/.media .stream-->

@endforelse

Is there any error in the code i can't see? 我看不到的代码有错误吗?

Found the bug in the code, though I had to use JavaScript's alert method to print the value of $(window).height(), $(document).height() and $(window).scrollTop(). 虽然我必须使用JavaScript的alert方法来打印$(window).height(),$(document).height()和$(window).scrollTop()的值,但在代码中找到了该错误。

That was when I discovered that I missed the parentheses after $(window).scrollTop. 那是当我发现我错过了$(window).scrollTop之后的括号。

The corrected code is-> 正确的代码是->

var scroll_position_for_post_load = $(window).height() + $(window).scrollTop() + 100;

if(scroll_position_for_post_load >= $(document).height(){
    // rest of code goes here
}

AJAX 在滚动到底部时加载更多<div></div><div id="text_translate"><p>我想使用 AJAX 和 PHP ZC1C425268E68385D14AB5074C17A 从 mysql 加载数据。 例子: </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-css lang-css prettyprint-override"> #ajaxload { border: 1px solid #000; max-height: 400px; overflow-y:scroll; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;?php //data from mysql... ?&gt; &lt;div id="ajaxload"&gt; &lt;ul&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;Loading next data...&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt;</pre></div></div><p></p><p> 但是我在这里(在stackoverflow上)没有找到具体的例子......一切都只是全页滚动,但我只想滚动&lt;div&gt;以加载更多数据。 你能帮助我吗?</p></div> - AJAX load more on scroll to bottom in <div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery滚动加载更多数据而不加载更多数据 - jQuery load more data on scroll not loading more data 当滚动位置在底部时,jQuery将加载更多数据 - jQuery Load More Data When Scroll Position at Bottom AJAX 在滚动到底部时加载更多<div></div><div id="text_translate"><p>我想使用 AJAX 和 PHP ZC1C425268E68385D14AB5074C17A 从 mysql 加载数据。 例子: </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-css lang-css prettyprint-override"> #ajaxload { border: 1px solid #000; max-height: 400px; overflow-y:scroll; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;?php //data from mysql... ?&gt; &lt;div id="ajaxload"&gt; &lt;ul&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;data...&lt;/li&gt; &lt;li&gt;Loading next data...&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt;</pre></div></div><p></p><p> 但是我在这里(在stackoverflow上)没有找到具体的例子......一切都只是全页滚动,但我只想滚动&lt;div&gt;以加载更多数据。 你能帮助我吗?</p></div> - AJAX load more on scroll to bottom in <div> 加载更多选项在 Laravel AJAX 中不起作用 - Load More option not working in Laravel AJAX 向下滚动以加载更多搜索数据 - Load more search data on scroll down 无限 Ajax jQuery 错误在窗口滚动时加载更多(重复数据)问题 - Infinite Ajax jQuery bug where on window scroll it loads more (duplicates data) issue 如何解决jQuery ajax加载更多的问题? - How to solve jQuery ajax load more issue? 无限滚动:从数据库加载所有数据并滚动以显示更多 - infinity scroll: Load all data from database and scroll to show more 没有响应数据ajax jquery laravel - No response data ajax jquery laravel 如何更改Ajax分页以加载更多或无限滚动功能 - How to change ajax pagination to load more or infinite scroll functionality
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM