如何确定垂直滚动条是否已到达网页底部?

时间:2011-05-27 06:38:41

标签: javascript window scrollbar

在jquery中回答了同样的问题,但我正在寻找没有jQuery的解决方案。 How do you know the scroll bar has reached bottom of a page

我想知道如何确定垂直滚动条是否已到达网页的底部。

我正在使用Firefox3.6

我编写了简单的Javascript循环向下滚动200像素,当滚动条到达页面底部时,我想停止循环。

问题是 scrollHeight()返回1989年。

内部循环scrollTop每次迭代增加200。

200 ==> 400 ==> 600 .... 1715

从1715年开始,它不会增加,所以这个循环会永远持续下去。

看起来像scrollHeight()和scrollTop()是不正确的比较方式,以确定滚动条的实际位置?我怎么知道循环何时停止?

var curWindow = selenium.browserbot.getCurrentWindow();
var scrollTop = curWindow.document.body.scrollTop;
alert('scrollHeight==>' + curWindow.document.body.scrollHeight);

    while(curWindow.document.body.scrollHeight > curWindow.document.body.scrollTop) {
      scrollTop = curWindow.document.body.scrollTop;
      if(scrollTop == 0) { 
        if(window.pageYOffset) { //firefox
          alert('firefox'); 
          scrollTop = window.pageYOffset; 
        } 
        else { //IE
          alert('IE'); 
          scrollTop = (curWindow.document.body.parentElement) ? curWindow.document.body.parentElement.scrollTop : 0; 
        } 
      } //end outer if
      alert('current scrollTop ==> ' + scrollTop);
      alert('take a shot here'); 
      selenium.browserbot.getCurrentWindow().scrollBy(0,200);

    } //end while

6 个答案:

答案 0 :(得分:8)

当你告诉一个元素滚动时,如果它的scrollTop(或任何适当的属性)没有改变,那么你不能认为它已经滚动到有能力吗?

所以你可以跟踪旧的scrollTop,告诉它滚动一些,然后检查它是否真的这样做了:

function scroller() {
    var old = someElem.scrollTop;
    someElem.scrollTop += 200;
    if (someElem.scrollTop > old) {
        // we still have some scrolling to do...
    } else {
        // we have reached rock bottom
    }
}

答案 1 :(得分:7)

我刚刚阅读了jQuery源代码,看起来你需要“pageYOffset”。然后你可以得到窗口高度和文档高度。

这样的事情:

var yLeftToGo = document.height - (window.pageYOffset + window.innerHeight);

如果yLeftToGo为0,那么你就在底部。至少这是一般的想法。

答案 2 :(得分:4)

function scrollHandler(theElement){
   if((theElement.scrollHeight - theElement.scrollTop) + "px" == theElement.style.height)
       alert("Bottom");
}

对于HTML元素(如div),添加事件 - onscroll ='scrollHandler(this)'。

答案 3 :(得分:4)

检查您是否到达页面底部的正确方法是:

  1. 获取document.body.clientHeight =显示的ACTUAL屏幕的高度
  2. 获取document.body.offsetHeight或document.body.scrollHeight =显示的整个页面的高度
  3. 检查document.body.scrollTop = document.body.scrollHeight - document.body.clientHeight
  4. 如果3为真,则到达页面底部

答案 4 :(得分:1)

以下是我用来为无限滚动列表视图提供动力的一些代码:

var isBelowBuffer = false;  // Flag to prevent actions from firing multiple times

$(window).scroll(function() {
    // Anytime user scrolls to the bottom
    if (isScrolledToBottom(30) === true) {
        if (isBelowBuffer === false) {
            // ... do something
        }
        isBelowBuffer = true;
    } else {
        isBelowBuffer = false;
    }
});

function isScrolledToBottom(buffer) {
    var pageHeight = document.body.scrollHeight;
    // NOTE:  IE and the other browsers handle scrollTop and pageYOffset differently
    var pagePosition = document.body.offsetHeight + Math.max(parseInt(document.body.scrollTop), parseInt(window.pageYOffset - 1));
    buffer = buffer || 0;
    console.log(pagePosition + "px / " + (pageHeight) + "px");
    return pagePosition >= (pageHeight - buffer);
}

答案 5 :(得分:0)

<span id="add"></add>

<script>
    window.onscroll = scroll;

    function scroll () {
    if (window.pageYOffset + window.innerHeight >= document.body.scrollHeight - 100) {
            document.getElementById("add").innerHTML += 'test<br />test<br />test<br />test<br />test<br />';
        }
    }
</script>
相关问题