如何停止javascript函数页面滚动

时间:2014-06-12 07:46:37

标签: javascript function

我有一个简单的脚本可以在html网页上自动滚动:

     function pageScroll() {
          window.scrollBy(0,10); 
             scrolldelay = setTimeout('pageScroll()',20);
}

我希望当页面到达底部时滚动停止,以便用户可以轻松地返回顶部;我知道我可以使用函数pageScrollStop和clearTimeout,但我不知道如何。

非常感谢!

3 个答案:

答案 0 :(得分:0)

clearTimeout(scrolldelay);

应该做的伎俩

答案 1 :(得分:0)

也许你可以这样做:

var pageBottom;
function initScroll() {
    pageBottom = document.body.offsetHeight - window.innerHeight + document.getElementById("footer").offsetHeight + 20;
    pageScroll();
}
function pageScroll() {
    if (window.scrollY >= pageBottom) return false;
    window.scrollBy(0, 10);
    scrolldelay = setTimeout('pageScroll()', 20);
}

将body onload函数更改为initScroll()。

我这样做只是为了获得更好的性能,因为你可以看到pageBottom只计算一次,而不是每次调用pageScroll()...

答案 2 :(得分:0)

只需在pageScroll函数内评估滚动位置(jQuery做得很漂亮),如果页面已经到达底部,则通过清除超时退出递归循环

相关问题