我在获取附加 Bootstrap 导航栏以在页面滚动时正确调整其宽度时遇到问题。
我希望导航栏在页面滚动时保持其包含元素内部的宽度,或者占据页面的整个宽度(从左到右开始)
我做错了什么,或者我需要做什么?
fiddle :https://jsfiddle.net/btckkdkk/12/
<div class="container">
<div id="renderbody">
<div id="renderbody-inner">
<nav class="navbar navbar-default" role="navigation" data-spy="affix" data-offset-top="100" style="margin-bottom:5px">
<div class="col-sm-12">
<button type="button" class="navbar-toggle navbar-toggle-sm navbar-left" data-toggle="collapse" data-target="#storysubmit-navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<form action="#" class="navbar-form navbar-form-custom navbar-left" role="search" id="searchForm" data-results-target="#storyList">
<div class="input-group">
<input type="text" class="form-control input-sm" placeholder="Search" name="searchTerms" id="searchTerms" />
<div class="input-group-btn">
<div class="btn-group">
<button type="submit" class="btn btn-sm btn-default"><i class="glyphicon glyphicon-search"></i></button>
</div>
<a href="#" class="btn btn-sm btn-link" style="margin-right:30px">Advanced</a>
</div>
</div>
</form>
<div class="navbar-form navbar-right collapse navbar-collapse" style="padding-right: 15px; padding-left: 15px; margin-right:0" id="storysubmit-navbar">
<a href="#" class="btn btn-default btn-sm">
<i class="glyphicon glyphicon-pencil margin-right-5" aria-hidden="true"></i>
Submit
</a>
</div>
</div>
</nav>
<div style="height:500px; background-color:darkgray;">
</div>
<div style="height:500px; background-color:lightgray;">
</div>
<div style="height:500px; background-color:darkgray;">
</div>
<div style="height:500px; background-color:lightgray;">
</div>
</div>
</div>
</div>
最佳答案
如何控制固定导航栏的宽度
The Affix plugin通过添加或删除 .affix
类来控制导航栏的行为。此类应用 position: fixed;
属性。因此,您可以使用特殊属性来控制导航栏的宽度:
For absolutely positioned elements (those with
position: absolute
orposition: fixed
), it specifies the distance between the margin edge of the element and the edge of its containing block.For relatively positioned elements (those with
position: relative
), it specifies the amount the element is moved below its normal position.
当 Navbar 是固定
位置时,它的位置是相对于浏览器窗口计算的。
100% 的窗口
我已将此 CSS 添加到您的 https://jsfiddle.net/btckkdkk/8/ :
.navbar.affix {
left: 0;
right: 0;
width: auto;
}
此代码在窗口的左右边缘之间拉伸(stretch)导航栏。
查看结果:https://jsfiddle.net/glebkema/essb11a1/
父 block 的宽度
您需要计算面板相对于浏览器窗口的宽度和位置。然后,您将能够模拟面板在特定 block 中的位置。
您可以为每个 width of the container 设置导航栏的属性通过媒体查询。公式为
90% * (width of the container - 2 * padding of the container)
所以代码是
.navbar.affix {
left: calc(15px + 5%);
right: calc(15px + 5%);
width: auto;
}
@media (min-width: 768px) {
.navbar.affix {
left: 50%;
right: auto;
width: 648px;
transform: translateX(-50%);
}
}
@media (min-width: 992px) {
.navbar.affix {
width: 846px;
}
}
@media (min-width: 1200px) {
.navbar.affix {
width: 1026px;
}
}
关于javascript - Bootstrap Navbar(贴)动态宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37925768/