css - Bootstrap 4:内容重叠的固定位置侧边栏
问题描述
长话短说:我正在使用 SB Admin 2 模板构建一个网站,并进行了一些小的修改,我想要更改的一件事是固定左侧边栏,以便在滚动时保持原位。将 .position-fixed 添加到主 UL 包装器就可以做到这一点,但同时它会导致紧跟 UL 的 #content-wrapper div 跨越页面的整个宽度并与侧边栏重叠。我已经为#content-wrapper 使用了margin-left 值,但它太依赖于屏幕分辨率。另外,我希望在侧边栏折叠时扩展内容包装器,就像在香草 SB Admin 2 中一样。任何提示都表示赞赏。侧边栏和内容包装类目前如下:
<ul class="navbar-nav bg-gradient-secondary sidebar sidebar-dark accordion position-fixed" id="accordionSidebar">
<div id="content-wrapper" class="d-flex flex-column">
解决方案
美好的。通过在 CSS 中添加 #wrapper #content-wrapper {margin-left: 14rem;} 并为侧边栏折叠按钮编写这段 jQuery 自己解决了这个问题。似乎在所有分辨率下都能完美运行:
$('#sidebarToggle').click( function(e) {
if ($("#accordionSidebar").hasClass("toggled")) {
$('#content-wrapper').css({'margin-left': "6.5rem"});
} else {
$('#content-wrapper').css({'margin-left': "14rem"});
}
});
推荐阅读
- spring - 在已安装并运行 Tomcat 时使用 Spring Framework Web
- c# - LINQ 查询将对象的两个值与列表进行比较,其中两个对象必须匹配
- python - 如何在熊猫中使用自定义权重计算移动平均值?
- node.js - Angular CORS 问题 - 每 5 秒查询一次端点
- php - 通过 curl 命令电报机器人触发 PHP 脚本
- java - 管道因无效的命名空间字符串而失败:'//'
- html - 无论先前元素的内容如何变化,如何对齐按钮?
- sql - 解码不工作
- javascript - Javascript将对象转换为另一个复杂的变体
- python-3.x - 如何根据另一个数据框条件替换数据框列中的值