jQuery load content when scroll to bottom-100px of page, multiple events fired

后端 未结 3 2057
时光说笑
时光说笑 2020-12-02 18:00

I want an event to load more content when a user is scrolling and reaches nearly the bottom of the page, say about 100px from the bottom, the problem is that the events get

相关标签:
3条回答
  • 2020-12-02 18:24

    I had a same problem. To solve it, Im using underscore.js library to prevent multiple events from firing. Here is the link. http://underscorejs.org/#throttle

    0 讨论(0)
  • 2020-12-02 18:27

    This will help you.

    <script type="text/javascript">
    var sIndex = 11, offSet = 10, isPreviousEventComplete = true, isDataAvailable = true;
    
    $(window).scroll(function () {
     if ($(document).height() - 50 <= $(window).scrollTop() + $(window).height()) {
      if (isPreviousEventComplete && isDataAvailable) {
    
        isPreviousEventComplete = false;
        $(".LoaderImage").css("display", "block");
    
        $.ajax({
          type: "GET",
          url: 'getMorePosts.ashx?startIndex=' + sIndex + '&offset=' + offSet + '',
          success: function (result) {
            $(".divContent").append(result);
    
            sIndex = sIndex + offSet;
            isPreviousEventComplete = true;
    
            if (result == '') //When data is not available
                isDataAvailable = false;
    
            $(".LoaderImage").css("display", "none");
          },
          error: function (error) {
              alert(error);
          }
        });
    
      }
     }
     });
     </script>
    

    Go through this link for whole article and details how to fire multiple event.

    load more content when browser scroll to end of page in jquery

    0 讨论(0)
  • 2020-12-02 18:36

    I was having this same problem too. I came across this link and it really helped (and worked)!

    Update: I looked at the code and I think that when you rebind, you are missing the function to rebind to.

    jsFiddle

     function loadMore()
    {
       console.log("More loaded");
        $("body").append("<div>");
       $(window).bind('scroll', bindScroll);
     }
    
     function bindScroll(){
       if($(window).scrollTop() + $(window).height() > $(document).height() - 100) {
           $(window).unbind('scroll');
           loadMore();
       }
    }
    
    $(window).scroll(bindScroll);
    
    0 讨论(0)
提交回复
热议问题