问题描述
在jquery中回答了同样的问题,但我正在寻找没有jQuery的解决方案。
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
我想知道如何确定垂直滚动条是否已到达底部网页。
我正在使用 Firefox3.6
我写了简单的Javascript循环向下滚动200像素,当滚动条到达页面底部时,我想停止循环。
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.
问题是 scrollHeight()返回1989年。
内部循环 scrollTop
每次迭代增加200。
And inside loop scrollTop
is incremented by 200 per iteration.
200 ==> 400 ==> 600 .... 1715
从1715开始,它不会增加,所以这个循环会永远持续下去。
And from 1715, it won't increment so this loop continues forever.
看起来像scrollHeight()和scrollTop()是不正确的比较方式,以确定滚动条的实际位置?我怎么知道循环何时停止?
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?
代码:
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
推荐答案
当你告诉一个元素滚动时,如果它的 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?
所以你可以跟踪旧的 scrollTop
,告诉它滚动一些,然后检查看它是否真的这样做了:
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
}
}
这篇关于如何确定垂直滚动条是否已到达网页底部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!