2013-07-30 131 views
2

如何检测Div时滚动条,滚动起来,打顶(只有当它击中顶部)检测DIV滚动条打在上面

我已经找到了另一篇文章,但是这一次是见底。

我需要的是击中顶部。任何人都知道如何改变这一点?

$("#divID").scroll(function(){ 
    if($(this)[0].scrollHeight - $(this).scrollTop() <= $(this).outerHeight()) 
    { 
     alert(1); 
    } 
}); 

Know when vertical scrollbar hits bottom of scroll in div

+1

这似乎很复杂,为什么不尝试一些事情简单! –

回答

3

working fiddle

var el = $('.test'); 
el.on('scroll', function(){ 

    if(el.scrollTop() == 0){alert("i just hit the top")} 


}); 
+0

哇!多谢! – Ben

+0

@ BenWong:http://jsfiddle.net/6A6qy/1/ for window ... –

+0

需要等待,stackoverflow不要让我接受太早 – Ben

1

scrollTop0当滚动条是顶部。试试这个:

$("#divID").scroll(function() { 
    if ($(this).scrollTop() == 0) { 
     alert(1); 
    } 
}); 

Example fiddle

0
$("#divID").scroll(function(){ 
    if($(this)[0].scrollHeight - $(this).scrollTop() <= $(this).outerHeight()) 
    { 
     alert("Bottom"); 
    } 
    if($(this).scrollTop() == 0) 
    { 
     alert("Top"); 
    } 
}); 

小提琴是here