递归window.scrollTo在FireFox中不起作用。 JavaScript的

时间:2013-11-17 13:08:04

标签: javascript firefox

我正在使用此功能“onclick”向下滚动页面:

function moveDown(arg) {
    smooth = setTimeout(moveDown, 10);
    if(!isFirefox){     ///// This works good for Chrome
         window.scrollTo(0, window.pageYOffset + 1);
    }
    if(isFirefox){    /// Does not work in FireFox
         console.log(window.pageYOffset) /// Every iteration returns 0
         window.scrollTo(0, window.pageYOffset + 1);
         console.log(window.pageYOffset) /// Every iteration returns 1
    }

逻辑:我们每次迭代都会将pageYOffset增加1 px,但每次迭代时它再次为0。谢谢!

我发布了完整的代码,因为我自己找不到问题。

function moveDown(arg) {
    offset = parseFloat(timeline.style.width) * 7100 / 100;
    setTimeout(function() {  /// 1 ms - time to calculate offset
        window.scroll(0, offset);
    }, 1);
    if ( typeof arg === 'undefined') {  /// makes argument optional
        arg = true;
    }
    if (arg) {
            smooth = setTimeout(moveDown, 10); // Timeout makes recursive calls 
            if (window.pageYOffset > 7100) {
                story.pause();
                isPlaying = false;
                enable_scroll();
                animateTimeLine(false);
                clearTimeout(smooth);
                playButton.style.display = "block";
                pauseButton.style.display = "none";

            }
            if(!isFirefox){
                window.scrollTo(0, window.pageYOffset + 7408 / (story.duration * 47));
            }
            if(isFirefox){
                console.log(window.pageYOffset);
                window.scrollTo(0, window.pageYOffset + 7408 / (84 * 47));
                console.log(window.pageYOffset);
            }
    }
    if (arg == false) {
        clearTimeout(smooth);
    }
}

playButton.onclick = function() {
    moveDown();
}

1 个答案:

答案 0 :(得分:0)

你在Firefox中有滚动条吗?您必须确保浏览器可以滚动。没有空间它将无法工作。

此代码每隔200ms向下滚动1px,经过测试并可在Firefox v25中运行:

<html>
<body onload="window.scrollTo(0,0); smooth = setInterval(moveDown, 200);">
<div style="height:2000px;">scroll</span>
<script>
function moveDown() {
     console.log(window.pageYOffset);
     window.scrollTo(0, window.pageYOffset + 1);
     console.log(window.pageYOffset);
}
</script>
</html>