2013-09-25 38 views
0
function yHandler() { 

    var show = document.getElementById('show'); 
    var contentHeight = show.offsetHeight; 
    var yOffset = show.pageYOffset; 
    var y = yOffset + show.innerHeight; 

    if(y >= contentHeight) { 
     alert("ok") 
     } 
    } 
    show.onscroll = yHandler; 

如何检查滚动条是否已达到div的结尾?如何检查滚动条是否已达到div的末尾?

+0

http://stackoverflow.com/questions/8480466/how-to-check-if-scrollbar-is-at-the-bottom –

回答

5

一些代码为你工作:

var scroll = document.getElementById('scroll'); 
var content = document.getElementById('content'); 

scroll.onscroll = function(){ 
    var total = scroll.scrollTop + scroll.clientHeight; 

    if(total == content.clientHeight) 
     alert('Reached bottom!'); 
} 

http://jsfiddle.net/EY6qP/

1

托尔的方法工作得很好(+1),但你也可以依靠scrollHeight

(function(scroll){ 
    scroll.onscroll = function(){ 
    if (scroll.scrollTop + scroll.clientHeight == scroll.scrollHeight) { 
     console.log('hither!'); 
    } 
    } 
})(document.getElementById('scroll')); 
+0

的可能的复制是的,这看起来像一个更优雅的解决方案,用于肯定:)我的只是一个快速的概念证明,那些往往是丑陋的;) –

+1

语法错误的人:/ –

+0

@rexhin干杯,应该现在修复。 – Pebbl