JQuery ScrollTop 不适用于 Overflow

标签 jquery html css scroll

在构建我的移动网站时,我必须将这些属性添加到我的正文中以消除多余的空白:

html,body
{
    width: 100%;
    margin: 0px;
    padding: 0px;
    overflow-y: scroll;
    overflow-x: hidden; 
    -webkit-overflow-scrolling: touch;
}

但是,我的 jQuery scrollTop 函数(关于视差和导航栏)现在不起作用。这些功能不适用于移动设备。

$n = ".navbar";
$(window).scroll(function(){
    if($(window).scrollTop() > 300){
        $($n).css("background-color", "rgba(255,255,255,.2)");
    } else {
        $($n).css("background-color", "transparent");
    }
});

我已经尝试从正文中删除溢出,并将其仅保留在 HTML 上,反之亦然,这确实解决了我的问题,但是空白返回了。我在网上看过,但我似乎找不到任何关于这个问题的信息。

如果要查看网站,网址为http://studysesh.us . 请记住,这才刚刚开始,目前主页是唯一的页面。 谢谢。

最佳答案

通过删除 body 上的 height 属性解决了这个问题,这实际上相当于将 height 设置为 auto.

body {
   height: auto;
   max-width: 100%;
}

When you set a value to the height of the body explicitly, scrolling on any area over it would actually trigger scroll event inside the body itself, and not the window. In my opinion, this was the source of the problem with the scroll event not being fired.

与此同时, overflow-y: scroll; 在您的情况下是多余且不必要的,因为它会在视口(viewport)的宽度时在 body 上强制使用丑陋的滚动条显着减少。

关于JQuery ScrollTop 不适用于 Overflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37678457/

相关文章:

css - 试图将所有 scss 文件编译成一个 css 文件

html - 背景大小在 IE 中不起作用

html - CSS 页脚麻烦 - 保持下来

javascript - jquery slideDown 和 slideUp() 不适用于部分显示

jquery - Bootstrap 表更新内容不起作用

javascript - 隐藏带有html字符串的div并将内容移动到span,然后将字符串编码为html

javascript - 使用 JQuery AJAX 发送包含文件和附加 json 表单数据的多部分/表单数据

html - 容器底部的额外空间

javascript - Chrome 中 HTML DOM 上的断点

javascript - 从另一个网站导入 <template> 元素