html - 内容显示滚动条但不可滚动

标签 html css

我不希望内容向上滚动,但它永远不应该在菜单后面可见。 这很难,因为它是透明的。所以我用一个 div 把它当作 mask 。 但出于某种原因,我的内容无法滚动。

HTML:

<div class="title">
    title
</div>
<div class="menu">
    menu
    <button class="btn">Foo</button>
</div>
<div class="content-mask">
  <div class="asfsadf">
    scroll content<br/>
    scroll content<br/>
    scroll content<br/>
    scroll content<br/>
    scroll content<br/>
  </div>
</div>

CSS:

.title {
   position: fixed; 
   top: 0;
}
.menu {
   position: fixed; 
   top: 20px;
   border: 1px solid black;
}
.content-mask {
   position: fixed; 
   top: 40px;
   overflow: scroll;
}
.content {
}
body {
   background-image:  url("https://pbs.twimg.com/media/ChtN47lVAAAQWD1.jpg:large");
}

https://jsfiddle.net/clankill3r/34Lf1mke/

最佳答案

为 .content-mask 添加高度:

.content-mask {
  position: fixed; 
  top: 40px;
  overflow: scroll;
  height: calc(100vh - 40px);
}

https://jsfiddle.net/8ha45uao/

或者只是添加 height 和 overflow: auto 到内容容器:

.asfsadf {
  margin-top: 40px;
  height: calc(100vh - 40px);
  overflow: auto;
}

https://jsfiddle.net/34Lf1mke/3/

如果你关心旧浏览器,你可以让 javascript fallback 来计算高度。

关于html - 内容显示滚动条但不可滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37087247/

相关文章:

html - 使用 meta http-equiv ="refresh"和 gh-pages 的相对重定向

javascript - Bootstrap 4 sticky-top margin-top

CSS:z-index 不能在位置内工作:相对容器

html - 需要帮助理解 "position"和 "float"的 css

html - CSS:为给定高度保持元素的纵横比

html - 导航栏中的不同网站链接

javascript - 如何检测div元素溢出?

javascript - 导航 ul li a 检测哪个被点击并做某事

html - WrapBootstrap 模板不起作用

html - 速记属性中的缺失值