文档末尾的事件

Event at the end of the document

本文关键字:事件 文档      更新时间:2023-09-26

我不明白为什么这不起作用。请解释。

 var scrollTop = $(window).scrollTop();
    var height = $(window).height();
    if (scrollTop === height ) {
        alert("end!");
    }

http://jsfiddle.net/Zasxe/

$(window).height()提供的是用户视口的大小,而不是您最想要的整个文档的大小。要查找用户是否已到达文档末尾,您必须按$(document).height()查找文档的大小。

此外,要使用jQuery查找用户是否已到达文档末尾,您需要捕获scroll事件,并查看用户是否已达到底部:

$(window).scroll(function () {
    if ($(window).scrollTop() + $(window).height() === $(document).height()) {
        alert("end!");
    }
});

没有事件处理程序。该代码只在页面加载时运行一次。

$(document).scroll(function () {
    // Your code here
});

请注意,每次滚动时代码都会运行,所以不要让它太重。

正如其他人所说,代码还有很多其他问题,但首先要放入处理程序。