最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to check if the scrollbar has reached at the end of div? - Stack Overflow

programmeradmin0浏览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;

how to check if the scrollbar has reached the end of div?

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;

how to check if the scrollbar has reached the end of div?

Share Improve this question asked Sep 25, 2013 at 12:47 M1XM1X 5,37413 gold badges65 silver badges129 bronze badges 1
  • Possible duplicate of stackoverflow./questions/8480466/… – Vignesh Paramasivam Commented Sep 25, 2013 at 13:01
Add a ment  | 

3 Answers 3

Reset to default 6

Some code for you to work on:

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/EY6qP/

Thor's method works perfectly well (+1), but you could also rely on scrollHeight.

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

Use scrollHeight, scrollTop and clientHeight attributes to detect the scroll bar reached bottom end or not.

function handleScroll() {
    var div = document.getElementById("div-id");
    if(Math.abs(Math.round(div.scrollHeight - div.scrollTop) - div.clientHeight) > 3) {
        console.log("Scroll bar reached bottom end");   
        return true;
    }
    return false;
};
发布评论

评论列表(0)

  1. 暂无评论