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)
根据@ 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)
结合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是合理的).
| 归档时间: |
|
| 查看次数: |
27259 次 |
| 最近记录: |