Erg*_*gec 61

这应该在没有插件的情况下做同样的技巧

$(window).scroll(function () { 
   if ($(window).scrollTop() >= $(document).height() - $(window).height() - 100) {
      //Add something at the end of the page
   }
});
Run Code Online (Sandbox Code Playgroud)

编辑2014年1月15日

根据@ pere的评论,最好使用下面的代码来避免过多的事件触发.

灵感来自这个答案/sf/answers/930861291/

var scrollListener = function () {
    $(window).one("scroll", function () { //unbinds itself every time it fires
        if ($(window).scrollTop() >= $(document).height() - $(window).height() - 100) {
            //Add something at the end of the page
        }
        setTimeout(scrollListener, 200); //rebinds itself after 200ms
    });
};
$(document).ready(function () {
    scrollListener();
});
Run Code Online (Sandbox Code Playgroud)

  • 我想链接一个[jQuery的作者John Resig博客文章](http://ejohn.org/blog/learning-from-twitter/)关于最佳实践.他建议不要在窗口滚动事件中做太多'(比如操纵DOM,或做一个AJAX调用),因为当用户滚动页面时,事件**将多次激活**. (9认同)

Alf*_*ton 7

结合Ergec的回答和Pere的评论:

function watchScrollPosition(callback, distance, interval) {
    var $window = $(window),
        $document = $(document);

    var checkScrollPosition = function() {
        var top = $document.height() - $window.height() - distance;

        if ($window.scrollTop() >= top) {
            callback();
        }
    };

    setInterval(checkScrollPosition, interval);
}
Run Code Online (Sandbox Code Playgroud)

distance 是回调将触发时屏幕底部的像素数.

interval 是检查运行的频率(以毫秒为单位; 250-1000是合理的).