2014-02-05 84 views
0
$(document).ready(function (e) { 
    sample(); // Initialize the function 
} 

function sample() { 
    $(document).scroll(function (e) { 
     var scroll_top = $(window).scrollTop(); 
     var document_bottom = scroll_top + $(window).height(); 
     var elementTop = $('.load-more').offset().top; 
     var elementBottom = (elementTop + $('.load-more').height() - 50) // 50 px offset; 
     if ((elementBottom <= document_bottom) && (elementTop >= scroll_top)) // User almost reach the end of visible items so load more data. Similar to infinite scroll 
     { 
      $(document).unbind("scroll"); // To prevent further requests until the load is complete 
      $.ajax({ 
       url: "sample", // Sampel URl for AJAX load 
       type: "GET", 
       data: { 
        start: 1 
       }, // Sample data 
       dataType: "html", 
       success: function (data) { 
        data = $(data); 
        $('#categories-rendered').append(data).masonry('appended', data); // Masonry append items 
        $(document).bind("scroll"); // To rebind the scroll event 
       } 
      }); 
     } 
    }); 
} // Sample function ends here 

重新绑定滚动不起作用。这个程序错了吗?jQuery重新绑定事件功能

回答

0

当前您将滚动事件重新绑定到无。

这里我已经提取了函数并创建了一个名为myfunction的新函数。与此功能

简明

var myfunction = function (e) {//Your logic}; 
$(document).bind("scroll", myfunction); 
$(document).unbind("scroll"); 

完整代码

$(document).ready(function (e) { 
    sample(); // Initialize the function 
}); 

function sample() { 
    $(document).scroll(myfunction); 

    //Create a seperate function 
    function myfunction(e) { 
     var scroll_top = $(window).scrollTop(); 
     var document_bottom = scroll_top + $(window).height(); 
     var elementTop = $('.load-more').offset().top; 
     var elementBottom = (elementTop + $('.load-more').height() - 50) // 50 px offset; 
     if ((elementBottom <= document_bottom) && (elementTop >= scroll_top)) // User almost reach the end of visible items so load more data. Similar to infinite scroll 
     { 
      $(document).unbind("scroll"); // To prevent further requests until the load is complete 
      $.ajax({ 
       url: "sample", // Sampel URl for AJAX load 
       type: "GET", 
       data: { 
        start: 1 
       }, // Sample data 
       dataType: "html", 
       success: function (data) { 
        data = $(data); 
        $('#categories-rendered').append(data).masonry('appended', data); // Masonry append items 

        //rebind with your function 
        $(document).bind("scroll", myfunction); // To rebind the scroll event 
       } 
      }); 
     } 
    } 
} // Sample function ends here 
0

尝试绑定滚动事件把事件中的一个功能:

var e = function (e) { ... }; 
$(document).bind("scroll", e); 
$(document).unbind("scroll", e); 

你的问题是使用一个匿名函数。