我能够在滚动到底部时加载我的ajax,我试图弄清楚如何修改下面的代码片段,以便它仅在窗口滚动到顶部时才起作用?
$(window).scroll(function () {
if ($(document).height() <= $(window).scrollTop() + $(window).height()) {
//this works here for scrolling bottom
}
else if ($(document).height() >= $(window).scrollTop() + $(window).height()){
//i tried checking for greater than the window scroll but that didn't owrk
}
});
最佳答案
当 scrollTop() 返回滚动条的垂直位置 0 表示滚动条位于顶部位置。
$(window).scroll(function () {
if ($(window).scrollTop() == 0){
alert("Up");
}
});
或者您可以按如下方式更新代码,
$(window).scroll(function () {
if ($(window).scrollTop() + $(window).height() < $(document).height()){
alert("Up");
//i tried checking for greater than the window scroll but that didn't work
}
});
关于javascript - JQuery 窗口滚动顶部和底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20233046/