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

时间:2013-09-25 12:47:09

标签: javascript

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的末尾?

2 个答案:

答案 0 :(得分:6)

您可以使用的一些代码:

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 :(得分:2)

Thor的方法效果非常好(+1),但您也可以依赖scrollHeight

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