html - 侧边栏滚动受后面内容影响
问题描述
我创建了一个侧边栏,其页面内容如下(示例):
<div id="sidebar">
<nav>
<ul>
<li>
<a href="#">Page 1</a>
</li>
<li>
<a href="#">Page 2</a>
</li>
</ul>
</nav>
</div>
<div class="content">
<h1>Main Content</h1>
<p>Lorem ipsum dolor sit amet ... More content</p>
</div>
和CSS:
#sidebar {
background-color: #e0e0e0;
border: 1px solid red;
overflow-x: hidden;
overflow-y: auto;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
opacity: 0.90; /* Used opacity to show content behind */
}
如果侧边栏内容很大,我需要侧边栏填满整个屏幕并垂直滚动......
但是如果后面的内容很大,我不希望侧边栏滚动......
在示例中,有 Y 滚动,因为即使侧边栏的内容不是,后面的内容也很大。
如何解决这个问题?
解决方案
有些可能是这样的:
body {
overflow-y: hidden;
}
#sidebar {
background-color: #e0e0e0;
border: 1px solid red;
overflow-x: hidden;
overflow-y: auto;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
opacity: 0.90;
height: 100vh;
overflow-y: auto;
}
<div id="sidebar">
<nav>
<ul>
<li>
<a href="#">Page 1</a>
</li>
<li>
<a href="#">Page 2</a>
</li>
</ul>
</nav>
</div>
<div class="content">
<h1>Main Content</h1>
<p>Lorem ipsum dolor sit amet ... More content</p>
</div>
推荐阅读
- c - c中的中止调用损坏的大小与prev_size错误
- regex - find 命令不能排除确切的正则表达式
- delphi - 如何准确计算备忘录中的单词?
- r - 从导入的 .csv 文件中删除 NaN 行
- html - 将列中的图像与容器对齐
- firebase - null 上的多提供者无效成员:提供者构造上的“集合”
- python-3.x - 如何验证用户的输入并确保它是正确的类型并在给定范围内?
- python - 在多项目列表中拆分字符串,同时将项目保持在一起
- ios - SwiftUI 内存管理
- node.js - 每当我尝试使用我的清除命令时,我的机器人在不和谐聊天中说“无效用户”在 cmd 或终端中没有错误消息