html - div内的垂直滚动?
问题描述
我有一个顶部有标头和汉堡菜单的移动网站。HTML 如下所示:
<div id="container">
<div id="cssmenu">...</div> // this is the collapsed burger menu with masthead
<div id="wrap">
<div id="title">...</div> // page title
<div id="content">...</div> // paragraph
</div>
</div>
#cssmenu 有固定的位置。因此,当用户向下滚动页面时,标题和内容实际上会在标头下方滚动,并导致页面锚点出现一些问题。有没有办法让#wrap 只在自己的 div 内垂直滚动?
解决方案
你也可以修复你的#wrap 容器。
#cssmenu {
position: fixed;
top: 0; left: 0; right: 0;
height: 30px;
background-color: hsla(0, 0%, 0%, 0.1);
}
#wrap {
position: fixed;
top: 30px; right: 0; bottom: 0; left: 0;
overflow-y: scroll;
}
#title,
#content {
height: 600px;
}
<div id="container">
<div id="cssmenu">menu</div>
<div id="wrap">
<div id="title">top of title</div>
<div id="content">top of content</div>
</div>
</div>
小提琴
https://jsfiddle.net/Hastig/e2qb9hu7/
您还可以使用基于视口高度的固定高度。
body {
height: 100vh;
margin: 0;
}
#cssmenu {
height: 10vh;
background-color: hsla(0, 0%, 0%, 0.1);
}
#wrap {
height: 90vh;
overflow-y: scroll;
}
#title,
#content {
height: 600px;
}
<div id="container">
<div id="cssmenu">menu</div>
<div id="wrap">
<div id="title">top of title</div>
<div id="content">top of content</div>
</div>
</div>
小提琴
推荐阅读
- git - `ls-remote --refs` 与克隆然后 `describe --tags` 有什么关系?
- plugins - Terraform 与插件不兼容的 API 版本。插件版本:5,核心版本:4
- powershell - 从另一个脚本运行 PowerShell 脚本,传递字符串变量
- azure-databricks - 我可以在不使用管道的情况下从 Azure Synapse 查询 SQL Server 数据库吗?
- javascript - A-frame 将跳跃高度设置为变量
- terminal - 终端 - 解压缩文件夹中的所有 .gz 文件,而不合并生成的文件
- angularjs - 如果电话号码无效,则阻止表单提交 - Angularjs
- json - 视图上的过滤器应用于视图中排除的过滤器
- c - 带有正则表达式和结构列表的分段错误(核心转储)
- javascript - 函数中的参数使用多个接口中的一个