基本上所有锚点href都有效,除了scrollspy不起作用。数据偏移也不起作用。只有点击第一个li
'主页'正在改变其背景,其他所有人都无法正常工作。当向下滚动到#search-nav
#obqvi
#about
时,与它们相关的菜单按钮也不会变色,这意味着我的scrollspy根本不起作用。
此外,当我单击其中一个锚点时,整个标题菜单按钮重新加载它会发生大约0.1秒,但它很难看。
http://getbootstrap.com/javascript/#scrollspy这是我用作模板的链接,与我的代码相比,我看不出任何差异
HTML:
<body data-spy="scroll" data-target=".navbar" data-offset="0">
<header>
<nav class="navbar navbar-fixed-top">
<div class="container">
<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="#"><img id="logo-img" src="images/logo.png" alt="logo"></a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li><a href="#">Home</a></li>
<li><a href="#search-nav"><span class="glyphicon glyphicon-search"></span> Search</a></li>
<li><a href="#obqvi"><span class="glyphicon glyphicon-home"></span> Sales</a></li>
<li><a href="#about"><span class="fa fa-cogs"></span> Services</a></li>
<li><a href="#footer-contact-form"><span class="glyphicon glyphicon-comment"></span> Contact</a></li>
<li><a href="#end-footer"><span class="glyphicon glyphicon-link"></span> About Us</a></li>
</ul>
</div>
</div>
</nav>
</header>
....
使用Jquery脚本编辑但是当我点击“a”时它仍然无法正常工作我可以顺利滚动到正确的位置 但是当我使用滚轮滚动时,与该部分相关的按钮不会变色并且在点击时它也不会变色
JS:
<script>
$(document).ready(function(){
$('.btn-information').click(function(){
$('#myModal').modal('show');
});
// Add scrollspy to <body>
$('body').scrollspy({ target: ".navbar", offset: 50 });
// Add smooth scrolling on all links inside the navbar
$("#myNavbar a").on('click', function (event) {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function () {
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
});
});
</script>
答案 0 :(得分:1)
<!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="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<style>
body {
position: relative;
}
#search-nav {
padding-top: 50px;
height: 500px;
color: #fff;
background-color: #1E88E5;
}
#obqvi {
padding-top: 50px;
height: 500px;
color: #fff;
background-color: #673ab7;
}
#about {
padding-top: 50px;
height: 500px;
color: #fff;
background-color: #ff9800;
}
#footer-contact-form {
padding-top: 50px;
height: 500px;
color: #fff;
background-color: #ff9800;
}
#end-footer {
padding-top: 50px;
height: 500px;
color: #fff;
background-color: #ff9800;
}
</style>
</head>
<body>
<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="#">WebSiteName</a>
</div>
<div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li><a href="#">Home</a></li>
<li><a href="#search-nav"><span class="glyphicon glyphicon-search"></span> Search</a></li>
<li><a href="#obqvi"><span class="glyphicon glyphicon-home"></span> Sales</a></li>
<li><a href="#about"><span class="fa fa-cogs"></span> Services</a></li>
<li><a href="#footer-contact-form"><span class="glyphicon glyphicon-comment"></span> Contact</a></li>
<li><a href="#end-footer"><span class="glyphicon glyphicon-link"></span> About Us</a></li>
</ul>
</div>
</div>
</div>
</nav>
<div id="search-nav" class="container-fluid">
<h1>Section 1</h1>
<p>Click on the different Section links in the navbar to see the smooth scrolling effect.</p>
</div>
<div id="obqvi" class="container-fluid">
<h1>Section 2</h1>
<p>Click on the different Section links in the navbar to see the smooth scrolling effect.</p>
</div>
<div id="about" class="container-fluid">
<h1>Section 3</h1>
<p>Click on the different Section links in the navbar to see the smooth scrolling effect.</p>
</div>
<div id="footer-contact-form" class="container-fluid">
<h1>Section 3</h1>
<p>Click on the different Section links in the navbar to see the smooth scrolling effect.</p>
</div>
<div id="end-footer" class="container-fluid">
<h1>Section 3</h1>
<p>Click on the different Section links in the navbar to see the smooth scrolling effect.</p>
</div>
<script>
$(document).ready(function () {
// Add scrollspy to <body>
$('body').scrollspy({ target: ".navbar", offset: 50 });
// Add smooth scrolling on all links inside the navbar
$("#myNavbar a").on('click', function (event) {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function () {
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
});
});
</script>
</body>
</html>
答案 1 :(得分:0)
我不知道它是否会有所帮助,但是当我使用滚动间谍功能时,我必须在我的js中包含以下内容。另外 - 检查你是否有滚动间谍所需的文件,并且jquery已加载并首先调入页面。
// jQuery for page scrolling feature - requires jQuery Easing plugin
$(function() {
$('a.page-scroll').bind('click', function(event) {
var $anchor = $(this);
$('html, body').stop().animate({
scrollTop: $($anchor.attr('href')).offset().top
}, 1500, 'easeInOutExpo');
event.preventDefault();
});
});
// Highlight the top nav as scrolling occurs
$('body').scrollspy({
target: '.navbar-fixed-top'
})