javascript - 查找页面的最大滚动位置

标签 javascript scroll

我已将页面主体设置为 200% 高,以便它适合屏幕两次。使用javascript,我让它在你滚动时一直滚动到顶部或底部。为此,我需要找出页面在任何浏览器或屏幕尺寸上的最低滚动点,以便它到达那里时停止。

请不要使用 JQuery。
谢谢。

我的代码:(它仍在组装中,因此需要一些工作)

function getScrollXY() {
    var x = 0, y = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        // Netscape
        x = window.pageXOffset;
        y = window.pageYOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        // DOM
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        // IE6 standards compliant mode
        x = document.documentElement.scrollLeft;
        y = document.documentElement.scrollTop;
    }
    return [x, y];
}

function scrollup() {
    if (xy[1] > 0) {
        window.scrollBy(0,-100);
        setTimeout(scrollup,200);
    } else {
        null;
    }
}

function scrolldown() {
    if (xy[1] < ) {
        window.scrollBy(0,100);
        setTimeout(scrolldown,200);
    } else {
        null;
    }
}

function dothescroll() {
    var xy = getScrollXY();
    var y = xy[1];
    setTimeout(function(){
        if (xy[1] > y) {
            scrollup();
        } else {
            scrolldown();
        }
    },200);
}

最佳答案

这是跨浏览器兼容版本:

var limit = Math.max( document.body.scrollHeight, document.body.offsetHeight, 
                   document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight );

关于javascript - 查找页面的最大滚动位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17688595/

相关文章:

javascript - Gulps gulp.watch 不会为新文件或已删除文件触发?

javascript - 根据窗口中的元素位置,向下滚动时淡入,向上滚动时淡出

c# - 如何使用 GDI+ 合法地滚动?

javascript - 在页面上搜索并返回图片

javascript - Map.forEach,为什么删除循环中的项目有效?

尽管 Promise.resolve() ,Promise catch block 中的 Javascript 重试异步函数始终运行

swift - TableView 内的 CollectionView 滚动ViewDidEndDecelerating 崩溃

javascript - Jquery 延迟滚动到#ID

javascript - 如何使用 jQuery 滚动到特定项目?

javascript - QUnit - 循环测试,索引始终相同