测试一些Bootstrap代码,无法使Scrollspy功能正常工作。
这是我的代码:
.box {
border:1px solid grey;
background-color:#d3d3d3;
}
.large {
font-size:3000%;
color:red;
}
#div1 { background-color: blue; }
#div2 { background-color: red; }
#div3 { background-color: green;}
.contentDiv {
height:800px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<body data-spy="scroll" data-target=".collapse">
<div class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<a href="" class="navbar-brand">My Website</a>
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#div1">Page 1</a></li>
<li><a href="#div2">Page 2</a></li>
<li><a href="#div3">Page 3</a></li>
</ul>
</div>
</div>
</div>
<hr>
<h1>Hello, world!</h1>
<div class="container">
<div id="div1" class="contentDiv">Content1</div><hr>
<div id="div2" class="contentDiv">Content2</div><hr>
<div id="div3" class="contentDiv">Content3</div><hr>
</div>
</body>
我遇到两个问题:
然而,它似乎适用于jsFiddle。
答案 0 :(得分:1)
您应该将position
(或您正在侦察的元素)的body
设为relative
。添加以下CSS规则
body
{
position: relative;
}
根据documentation,这是必要的。
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.js'></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<script type='text/javascript' src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<style type='text/css'>
body {position:relative;}
.box {border:1px solid grey;background-color:#d3d3d3;}
.large {font-size:3000%;color:red;}
#div1 {background-color:blue;}
#div2 {background-color:red;}
#div3 {background-color:green;}
.contentDiv {height:800px;}
</style>
</head>
<body data-spy="scroll" data-target=".collapse">
<div class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header"> <a href="" class="navbar-brand">My Website</a>
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#div1">Page 1</a></li>
<li><a href="#div2">Page 2</a></li>
<li><a href="#div3">Page 3</a></li>
</ul>
</div>
</div>
</div>
<hr>
<h1>Hello, world!</h1>
<div class="container">
<div id="div1" class="contentDiv">Content1</div>
<hr>
<div id="div2" class="contentDiv">Content2</div>
<hr>
<div id="div3" class="contentDiv">Content3</div>
<hr>
</div>
</body>
</html>
小提琴是here