当用户滚动到特定元素的结尾时如何运行jQuery代码?

时间:2022-10-08 02:23:09

Here is my sample code:

这是我的示例代码:

$(window).scroll(function () {
    if ($(window).scrollTop() >= $(document).height() - $(window).height()) {

            // Run code here...

    }
});

I use the above code to execute stuff when user scroll to bottom of page. Instead, how can I make the code executed when user scroll to bottom of specific element (e.g. #content) and not the whole document.

当用户滚动到页面底部时,我使用上面的代码来执行东西。相反,当用户滚动到特定元素(例如#content)的底部而不是整个文档时,如何执行代码。

Thank you.

1 个解决方案

#1


5  

This should probably work.

这应该可行。

$(window).scroll(function () {
    if ($(window).scrollTop() >= $(elem).position().top + $(elem).height() /* add padding if needed. */) {

            // Run code here...

    }
});

#1


5  

This should probably work.

这应该可行。

$(window).scroll(function () {
    if ($(window).scrollTop() >= $(elem).position().top + $(elem).height() /* add padding if needed. */) {

            // Run code here...

    }
});