Bootstrap Scrollspy&没有回应身体边缘的词缀

时间:2015-10-18 22:23:58

标签: javascript jquery html css twitter-bootstrap

我是新手,我正在为学校体验它。 但是我卡住了。我找到了一个使用Affix插件的漂亮导航栏示例,但是当您向下滚动时,不会应用我之前指定的相同边距规则。 不确定我是否正确解释,但我会给你一个截图。

情况1:向下滚动之前的情况(应该如何)

enter image description here

情况2:当我向下滚动时,会出现一个黑框,边距应为。

enter image description here

这是我的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>

1 个答案:

答案 0 :(得分:0)

实现这一目标的一种方法:

width: 100%课程中的affix更改为width: calc(100% - 4em);(因为您的身体上有2 + 2个边距)。