我很不熟悉引导程序,正在学校中体验。
但是我被卡住了。我使用Affix插件找到了一个不错的导航栏示例,但是当向下滚动词缀时,不会应用我之前指定的相同边距规则。
不知道我是否解释正确,但我将通过屏幕截图向您展示。
情况1:向下滚动之前的状态(应该如何)
情况2:当我向下滚动时,应该在右边距处出现一个黑框。
这是我的index.html(P.S. Snippet不起作用,因为它没有Bootstrap CSS和JS)
@charset "utf-8";
/* CSS Document */
body {
position: relative;
margin: 0 auto;
margin-left:2em;
margin-right:2em;
}
.affix {
top:0;
width: 100%;
z-index: 9999 !important;
}
.navbar {
margin-bottom: 0px;
}
.navbar-right {
margin-right: 20em;
}
.affix ~ .container-fluid {
position: relative;
top: 50px;
}
#biografie {
padding-top:50px;
height:40em;
color: #fff;
background-color: #1E88E5;
}
#portfolio {
padding-top:50px;
height:40em;
color: #fff;
background-color: #673ab7;
}
#contact {
padding-top:50px;
height:40em;
color: #fff;
background-color: #ff9800;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap - Prebuilt Layout</title>
<!-- Bootstrap -->
<link href="css/bootstrap.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet" type="text/css">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body data-spy="scroll" data-target=".navbar" data-offset="30">
<div class="container-fluid" style="background-color:#F44336;color:#fff;height:200px;">
<h1>Scrollspy & Affix Example</h1>
<h3>Fixed navbar on scroll</h3>
<p>Scroll this page to see how the navbar behaves with data-spy="affix" and data-spy="scrollspy".</p>
<p>The navbar is attached to the top of the page after you have scrolled a specified amount of pixels, and the links in the navbar are automatically updated based on scroll position.</p>
</div>
<nav class="navbar navbar-inverse" data-spy="affix" data-offset-top="197">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Homepage</a>
</div>
<div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li><a href="#biografie">Biografie</a></li>
<li><a href="#portfolio">Portfolio</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</div>
</div>
</div>
</nav>
<div id="biografie" class="container-fluid">
<h1>Biografie</h1>
<p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
<p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
</div>
<div id="portfolio" class="container-fluid">
<h1>Portfolio</h1>
<p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
<p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
</div>
<div id="contact" class="container-fluid">
<h1>Contact</h1>
<p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
<p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="js/jquery-1.11.2.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.js"></script>
</body>
</html>
最佳答案
一种实现方法:
将width: 100%
类中的affix
更改为width: calc(100% - 4em);
(因为您的身体有2 + 2ems的余量)。
关于javascript - Bootstrap Scrollspy和Affix没有响应并保留边距,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33203922/