引导滚动间谍不起作用,导航菜单按钮导致标题重新加载
Bootstrap scrollspy not working and nav menu buttons are causing the header to reload
基本上所有的锚点hrefs都可以工作,除了滚动间谍不工作。数据偏移也不起作用。单击时,只有第一个li
"主页"正在更改其背景,所有其他内容都不起作用。向下滚动到#search-nav
#obqvi
#about
等与它们相关的菜单按钮也没有变色,这意味着我的 scrollspy 根本不起作用。
此外,当我单击其中一个锚点时,整个标题菜单按钮会重新加载,它会发生大约 0.1 秒,但它很丑陋。
http://getbootstrap.com/javascript/#scrollspy 这是IM用作模板的链接,与我的代码相比,我看不到任何区别
.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>
<!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>
我不知道
它是否有帮助,但是当我使用滚动间谍功能时,我不得不在我的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'
})
相关文章:
- 数据表 AJAX 筛选器重新加载数据
- Bigcommerce自定义标题面板无法正确加载标题
- 如何在新加载的页面上执行(下拉)操作
- 根据上一个选项卡的选择器重新加载选项卡
- 获得“中止,因为不接受 0”并使用反应热加载器重新加载整页
- 咕噜咕噜的手表:实时加载重新加载落后 1 步....
- 在WordPress上加载新页面时重新加载标题
- 如何为新加载的元素提供JQuery UI工具提示
- 如何在新加载的页面上使用相同的JavaScript代码(通过重定向)
- 使用PHP或Javascript检测页面刷新或新加载
- 砌体重新加载&reloadItems不起作用
- 如何使javascript计时器重新加载
- 如何获取新加载的图像's width / naturalWidth
- 列出并调用firefox扩展中新加载的选项卡中的js函数
- 旋转器重新加载图像似乎在覆盖层后面
- AJAX调用后,新加载的CSS选择器对jQuery.ech()不可用
- jQuery砌体-砌体重新加载后调用方法
- 如何向所有查看器重新加载页面
- 如何打开子浏览器窗口,然后在新加载的子浏览器页面上执行脚本以单击其中的链接
- 如何选择新加载 html 的元素