2013-11-27 146 views
0

我能够加载我的ajax当滚动到底部时,我想弄清楚如何修改下面的代码片段,以便它只在窗口滚动时才起作用到顶部?JQuery窗口滚动顶部和底部

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

回答

3

scrollTop()返回滚动条的垂直位置就意味着滚动条处于顶部位置。

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

      } 
     }); 
+0

这不会解决问题,为的$(document).height()总是会在这种情况下,最大或同等价值与增加的其他数值相比。所以问题在于if-else-if的条件。 –

相关问题