繁体   English   中英

当html和body的高度为100%时,ScrollY / ScrollTop不起作用

[英]ScrollY/ScrollTop doesn't work when height of html and body is 100%

我正在使用height: 100%overflow: autohtml / body上修复Chrome上的滚动错误。 但是ScrollY / ScrollTop现在总是返回0。 在这种情况下有没有办法检查滚动位置?

HTML:

<html>
    <body>
        <div class="wrapper">
            <div class="top-nav">
            <div class="scroll-wrapper">
        </div>
    <body>
</html>

CSS:

html {
    height: 100%;
    overflow: auto;

    body {
        height: 100%;
        overflow: auto;

        .wrapper {
            .top-nav {
                z-index: 999;
                position: fixed;
                top: 0;
            }
            .scroll-wrapper {
                padding-top: 45px;
            }
        }
    }
}

JS:

    // Add style to top-nav when scrolling down

    $('.wrapper').scroll(function () {
        if (window.scrollY > 0)                        //always 0
        if ($('.wrapper')[0].scrollTop > 0)   
        if ($('.scroll-wrapper')[0].scrollTop > 0)
    }

编辑

看来你的问题实际上来自htmlhtml body都有overflow: scroll

尝试删除其中一个CSS规则,看看您的初始问题是否仍然已解决。

html {
    height: 100%;
    /* overflow: auto; */

    body {
        height: 100%;
        overflow: auto;

       /* ... */

    }
}

您还需要更改您的javascript以检查文档滚动而不是包装器。

$(document).scroll(function () {
    console.log(document.body.scrollTop)
    if (document.body.scrollTop > 0) {
        // Perform Action
    }
})

希望能解决你的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM