如何判断垂直滚动条是否已经到达网页底部?

发布于 2024-11-10 04:36:48 字数 1517 浏览 6 评论 0原文

jQUery 中回答了同样的问题,但我正在寻找没有 jQuery 的解决方案。 你怎么知道滚动条滚动条已到达页面底部

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

我使用的是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

The same question is answered in jQUery but I'm looking for solution without jQuery.
How do you know the scroll bar has reached bottom of a page

I would like to know how I can determine whether vertical scrollbar has reached the bottom of the web page.

I am using Firefox3.6

I wrote simple Javascript loop to scroll down by 200 pixel and when the scroll bar reached the bottom of the page, I want to stop the loop.

The problem is scrollHeight() is returning 1989.

And inside loop scrollTop is incremented by 200 per iteration.

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

And from 1715, it won't increment so this loop continues forever.

Looks like scrollHeight() and scrollTop() is not right way to compare in order to determine the actual position of scrollbar? How can I know when the loop should stop?

code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

太傻旳人生 2024-11-17 04:36:48

当您告诉一个元素滚动时,如果它的 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
    }
}

When you tell an element to scroll, if its scrollTop (or whatever appropriate property) doesn't change, then can't you assume that it has scrolled as far as is capable?

So you can keep track of the old scrollTop, tell it to scroll some, and then check to see if it really did it:

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
    }
}
二智少女 2024-11-17 04:36:48

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

像这样的东西:

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

如果 yLeftToGo 为 0,那么您位于底部。至少这是一般的想法。

I just read through the jQuery source code, and it looks like you'll need the "pageYOffset". Then you can get the window height and document height.

Something like this:

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

If yLeftToGo is 0, then you're at the bottom. At least that's the general idea.

瑶笙 2024-11-17 04:36:48

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

  1. Get document.body.clientHeight = 实际屏幕显示的高度
  2. Get document.body.offsetHeight或 document.body.scrollHeight = 显示的整个页面的高度
  3. 检查 document.body.scrollTop 是否为 document.body.scrollHeight - document.body.clientHeight

如果为 3真的,你到达页面底部

The correct way to check if you reached the bottom of the page is this:

  1. Get document.body.clientHeight = the height of the ACTUAL screen shown
  2. Get document.body.offsetHeight or document.body.scrollHeight = the height of the entire page shown
  3. Check if document.body.scrollTop = document.body.scrollHeight - document.body.clientHeight

If 3 is true, you reached the bottom of the page

北凤男飞 2024-11-17 04:36:48
function scrollHandler(theElement){
   if((theElement.scrollHeight - theElement.scrollTop) + "px" == theElement.style.height)
       alert("Bottom");
}

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

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

For the HTML element (like div) add the event -- onscroll='scrollHandler(this)'.

戏舞 2024-11-17 04:36:48

这是我用来支持无限滚动列表视图的一些代码:

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);
}

Here is some code I've used to power infinite scrolling list views:

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);
}
忆离笙 2024-11-17 04:36:48
<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>
<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文