2013-07-30 36 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