繁体   English   中英

jQuery窗口上下滚动

[英]JQuery window scroll top and bottom

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

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

 $(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())){

            }
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM