JQuery窗口顶部和底部滚动

时间:2013-11-27 02:55:44

标签: javascript jquery scroll

当我一直滚动到底部时,我能够加载我的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
    }
});

2 个答案:

答案 0 :(得分:3)

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
    }
});

答案 1 :(得分:0)

选中此项或者您应该检查文档和窗口对象的高度,以确保它们不为空。

 $(window).scroll(function () {
            if ($(document).height() <= Number($(window).scrollTop() + $(window).height())) {
               //this works here for scrolling bottom
            }
// only greater i think, not equa
            else if ($(document).height() > Number($(window).scrollTop() + $(window).height())){

            }
        });