2011-12-12 38 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

采取从这里。帮助我,因为我期待:) –