2011-12-12 40 views

回答

14

您會找到滾動容器的高度,然後將其與滾動位置進行比較。如果它們是相同的,那麼你已經達到了底部。

<div style="overflow: auto; height: 500px"> 
</div> 

$(document).ready(function() 
{ 
    $('div').scroll(function() 
    { 
     var div = $(this); 
     if (div.height() == div.scrollTop() + 1) //scrollTop is 0 based 
     { 
      alert('Reached the bottom!"); 
     } 
    }); 
}); 

編輯:在js小提琴中的一個小測試,我意識到以前的版本是不正確的。 可以使用DOM屬性來找出有多少滾動與元素的高度,像這樣

 var div = $(this); 
     if (div[0].scrollHeight - div.scrollTop() == div.height()) 
     { 
      alert('Reached the bottom!'); 
     } 

http://jsfiddle.net/Aet2x/1/

+0

[scrollHeight直到第8版才被引入到IE中](https://developer.mozilla.org/en/DOM/element.scrollHeight#Browser_compatibility) –

+1

@DavidHedlund如果他正在尋找IE 6和7兼容的方法,那麼這是行不通的,你是對的。他沒有提供關於瀏覽器支持要求的任何細節。 –

+0

這不適用於我 - 但是,如果我將它與div.innerHeight進行比較,它就會起作用。我假設保證金/填充是以這種方式進行的?我不完全確定。 – GlyphGryph

0

您可以檢查執行一些數學如果元素scrollTop等於元素innerHeight

if($('div').scrollTop() == $('div').innerHeight()){ 
    //Reached the bottom 
} 
2

這爲我工作(使用jQuery):

$(document).ready(function(){ 
    $('div').scroll(function(){ 
    //scrollTop refers to the top of the scroll position, which will be scrollHeight - offsetHeight 
    if(this.scrollTop == (this.scrollHeight - this.offsetHeight)) { 
     console.log("Top of the bottom reached!"); 
    } 
    }); 
}); 

here服用。

+0

採取從這裏。幫助我,因爲我期待:) –