<分区>
Possible Duplicate:
Detecting when user scrolls to bottom of div with jQuery
我有一个页面,其中有页眉、左侧菜单、内容、右侧菜单和页脚。在我的内容中,我有一个 div(比如 id-foo),其中有一些数据。当用户滚动页面并到达 div 的底部时,我必须进行 ajax 调用并带来更多数据以填充那里的内容。我如何检测到用户已滚动(页面滚动,div 中没有滚动)到 div (foo) 的底部。
标签 javascript jquery
<分区>
Possible Duplicate:
Detecting when user scrolls to bottom of div with jQuery
我有一个页面,其中有页眉、左侧菜单、内容、右侧菜单和页脚。在我的内容中,我有一个 div(比如 id-foo),其中有一些数据。当用户滚动页面并到达 div 的底部时,我必须进行 ajax 调用并带来更多数据以填充那里的内容。我如何检测到用户已滚动(页面滚动,div 中没有滚动)到 div (foo) 的底部。
最佳答案
您可能需要根据当前屏幕尺寸和 div 位置自己进行一些计算。代码大致如下所示(未经测试):
// Cache these values. If the div size changes, divBottom will need to be recalculated.
var $div = $("#yourdiv");
var divBottom = $div.offset().top + parseInt($div.height());
// Bind a scroll event for the whole page
$("body").bind("scroll", function(e)
{
// Calculate how far down the user has scrolled
var screenBottom = $(this).scrollTop() + parseInt($(window).height());
// Test if the div has been revealed
if(screenBottom > divBottom)
{
// do something
}
});
关于javascript - 检测用户是否滚动到 div 的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9983835/