如何判断垂直滚动条是否已经到达网页底部?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当您告诉一个元素滚动时,如果它的
scrollTop
(或任何适当的属性)没有改变,那么您不能假设它已经尽可能滚动了吗?因此,您可以跟踪旧的
scrollTop
,告诉它滚动一些,然后检查它是否真的做到了: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:我刚刚阅读了 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.
检查是否到达页面底部的正确方法是:
document.body.clientHeight
= 实际屏幕显示的高度document.body.offsetHeight
或 document.body.scrollHeight = 显示的整个页面的高度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:
document.body.clientHeight
= the height of the ACTUAL screen showndocument.body.offsetHeight
or document.body.scrollHeight = the height of the entire page showndocument.body.scrollTop
=document.body.scrollHeight - document.body.clientHeight
If 3 is true, you reached the bottom of the page
对于 HTML 元素(如 div)添加事件 --onscroll='scrollHandler(this)'。
For the HTML element (like div) add the event -- onscroll='scrollHandler(this)'.
这是我用来支持无限滚动列表视图的一些代码:
Here is some code I've used to power infinite scrolling list views: