【发布时间】:2018-04-06 15:45:16
【问题描述】:
向下滚动时,我正在尝试使用 Bootstrap 词缀将导航器(水平)固定到顶部。导航器在向下滚动时固定在顶部,但可滚动内容会在其上方而不是下方滚动。
更多背景知识... 主体,即可滚动内容是一种具有引导面板和使用 col-md-* 类来格式化布局的输入字段的表单。使用此类似乎是添加“位置:相对”属性,这似乎导致了重叠。
我的问题....我是 CSS 新手,所以
- 我不明白为什么位置:相对内容与位置:固定(导航器)重叠。
- 我不明白为什么 bootstrap 会添加 position:relative 到这些组件而没有额外的 top、left、bottom 或 right 属性。如果没有这些属性之一,这与默认的“位置:静态”不同吗?
- 最后,我该如何解决这个问题。我正在寻找一种在引导升级时不会中断的标准非黑客解决方案。
编辑:在起草这篇文章时,我有了一个想法,并为导航器添加了一个正的 z-index,可滚动内容现在在导航器后面滚动。这是正确的解决方案吗?不过,我仍在寻找其他问题的答案。谢谢。
*编辑 2:在这里找到了我所有词缀问题的答案。 https://www.jero.co.nz/blog/bootstrap-affix-plugin/ 另一个我在这里没有解决的问题......如果偏移量为零,则在点击和滚动时会被固定和不固定。将偏移量设置为 1。
现在我需要弄清楚如何将其标记为已解决。*
这是我使用 W3Schools 词缀示例创建的小提琴。 https://jsfiddle.net/fLx60zvh/2/
感谢您的帮助。
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
/* Note: Try to remove the following lines to see the effect of CSS positioning */
.affix {
top: 0;
width: 100%;
}
.affix + .container-fluid {
padding-top: 70px;
}
</style>
</head>
<body>
<div class="container-fluid" style="background-color:#F44336;color:#fff;height:200px;">
<h1>Bootstrap Affix Example</h1>
<h3>Fixed (sticky) navbar on scroll</h3>
<p>Scroll this page to see how the navbar behaves with data-spy="affix".</p>
<p>The navbar is attached to the top of the page after you have scrolled a specified amount of pixels.</p>
</div>
<nav class="navbar navbar-inverse" data-spy="affix" data-offset-top="197">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Basic Topnav</a></li>
<li><a href="#">Page 1</a></li>
<li><a href="#">Page 2</a></li>
<li><a href="#">Page 3</a></li>
</ul>
</nav>
<div class="container-fluid" style="height:1000px">
<div class="col-md-6">
<h1>Some text to enable scrolling</h1>
<h1>Some text to enable scrolling</h1>
<h1>Some text to enable scrolling</h1>
<h1>Some text to enable scrolling</h1>
<h1>Some text to enable scrolling</h1>
<h1>Some text to enable scrolling</h1>
<h1>Some text to enable scrolling</h1>
<h1>Some text to enable scrolling</h1>
<h1>Some text to enable scrolling</h1>
<h1>Some text to enable scrolling</h1>
<h1>Some text to enable scrolling</h1>
</div>
<div class="col-md-6">
<h1>Some text to enable scrolling</h1>
<h1>Some text to enable scrolling</h1>
<h1>Some text to enable scrolling</h1>
<h1>Some text to enable scrolling</h1>
<h1>Some text to enable scrolling</h1>
<h1>Some text to enable scrolling</h1>
<h1>Some text to enable scrolling</h1>
<h1>Some text to enable scrolling</h1>
<h1>Some text to enable scrolling</h1>
<h1>Some text to enable scrolling</h1>
<h1>Some text to enable scrolling</h1>
</div>
</div>
</body>
</html>
【问题讨论】:
-
是的,z-index 是正确的方式。
-
@grusl83 - 谢谢。总有一天,我会把这个 CSS 东西记下来。