侧边栏菜单展开/折叠时垂直滚动无法到底的问题排查
问题修复方案
问题根源
你的侧边栏滚动异常是因为布局高度计算和定位设置冲突:
- 使用
height:100vh但未固定上下定位,导致侧边栏位置偏移,部分区域被overflow:hidden裁剪 .content的max-height:100vh与flex布局的空间分配逻辑冲突,限制了滚动区域高度
修改后的CSS代码
.menu-links { position: fixed; top: 0; bottom: 0; left: -0.5rem; background-color: white; min-width: 17rem; transform: translateX(-17rem); overflow: hidden; transition: all 0.3s ease; display: flex; flex-direction: column; } .menu-links ul li a { font-size: 1.5rem; color: black; text-decoration: none; } .menu-links ul { display: flex; flex-direction: column; margin: 0; padding: 0; } .menu-links li { margin: 0.5rem 1rem; padding: 10px; list-style: none; } #sub { display: none; margin-top: 0.5rem; } #sub.open { display: block; } .content { overflow-y: auto; flex-grow: 1; padding: 1rem 0; }
修改说明
- 替换高度设置:用
top:0和bottom:0替代height:100vh,确保侧边栏完全贴合视口上下边缘,避免位置偏移导致的内容裁剪 - 优化滚动区域:移除
.content的max-height:100vh,依靠flex-grow:1让它自动填充侧边栏的剩余空间,配合overflow-y:auto实现完整滚动 - 清理默认样式:移除ul、li的默认margin、padding和列表圆点,避免多余间距影响布局
修改后,侧边栏可正常滚动到底部,展开Categories子菜单后也能查看所有子项及后续菜单选项。
内容的提问来源于stack exchange,提问作者Michael Oboho
相关产品推荐
相关产品推荐

