javascript - JQuery 窗口滚动顶部和底部

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

最佳答案

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/

相关文章:

jquery - CSS 叠加效果未关闭

当元素快速调整大小时,JavaScript offsetWidth 和 offsetHeight 给出错误的值

javascript - 如何显示错误消息以便 jquery 验证引擎?

php - 尝试根据 $_GET 变量淡入隐藏的 div

jquery mobile 1.4.2 在 pageContainer 更改后滚动到 div

scroll - Skrollr背景透明度?

javascript - ReactJS 获取 Virtual DOM 协调结果

JavaScript 表单到搜索引擎重定向

javascript - Highcharts折线图,在折线系列末尾显示系列名称

javascript - 如何在 Owl Carousel 中添加随机动画效果?