我的 bootstrap scrollspy 怎么了?
What's up with my bootstrap scrollspy?
我只想尝试 Bootstrap Navbar 的 scrollspy,但它不起作用。无论我单击哪个菜单,<li><a href="section4"></li>
项始终处于活动状态。如果我删除 body 中的 date-spy="scroll" data-target="#myNavbar" 和底部的脚本,li active 就可以了。
<head><meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<!--<link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css">-->
<link rel="stylesheet" type="text/css" href="css/style.css">
<script src="js/jquery-1.12.3.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/script.js?rand=<?php echo rand(); ?>"></script>
</head>
<body data-spy="scroll" data-target="#myNavbar">
<nav class="navbar navbar-inverse navbar-fixed-top">
<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="#">TEST</a>
</div>
<div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li><a href="#section1">Section 1</a></li>
<li><a href="#section2">Section 2</a></li>
<li><a href="#section3">Section 3</a></li>
<li><a href="#section4">Section 4</a></li>
</ul>
</div>
</div>
</div>
</nav>
<div id="section1" class="container-fluid">
<h1>Section 1</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="section2" class="container-fluid">
<h1>Section 2</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="section3" class="container-fluid">
<h1>Section 3</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="section4" class="container-fluid">
<h1>Section 4</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>
<script>
$('body').scrollspy({
target: '#myNavbar',
offset: 50
});
</script>
</body>
您需要将 jQuery 包装在文档就绪包装器中
$(document).ready(function(){
$('body').scrollspy({
target: '#myNavbar',
offset: 50
});
})
我还在其他文档中发现了这一点,您的 CSS:
中可能没有
需要相对定位:data-spy="scroll"的元素需要CSS位置属性,取值为"relative"正常工作。
我不知道为什么,html文件需要在文件顶部添加<!DOCTYPE html>
。并且 scrollspy 有效。
我只想尝试 Bootstrap Navbar 的 scrollspy,但它不起作用。无论我单击哪个菜单,<li><a href="section4"></li>
项始终处于活动状态。如果我删除 body 中的 date-spy="scroll" data-target="#myNavbar" 和底部的脚本,li active 就可以了。
<head><meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<!--<link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css">-->
<link rel="stylesheet" type="text/css" href="css/style.css">
<script src="js/jquery-1.12.3.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/script.js?rand=<?php echo rand(); ?>"></script>
</head>
<body data-spy="scroll" data-target="#myNavbar">
<nav class="navbar navbar-inverse navbar-fixed-top">
<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="#">TEST</a>
</div>
<div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li><a href="#section1">Section 1</a></li>
<li><a href="#section2">Section 2</a></li>
<li><a href="#section3">Section 3</a></li>
<li><a href="#section4">Section 4</a></li>
</ul>
</div>
</div>
</div>
</nav>
<div id="section1" class="container-fluid">
<h1>Section 1</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="section2" class="container-fluid">
<h1>Section 2</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="section3" class="container-fluid">
<h1>Section 3</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="section4" class="container-fluid">
<h1>Section 4</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>
<script>
$('body').scrollspy({
target: '#myNavbar',
offset: 50
});
</script>
</body>
您需要将 jQuery 包装在文档就绪包装器中
$(document).ready(function(){
$('body').scrollspy({
target: '#myNavbar',
offset: 50
});
})
我还在其他文档中发现了这一点,您的 CSS:
中可能没有需要相对定位:data-spy="scroll"的元素需要CSS位置属性,取值为"relative"正常工作。
我不知道为什么,html文件需要在文件顶部添加<!DOCTYPE html>
。并且 scrollspy 有效。