我有我的导航栏这个代码的网站,我做的:jQuery的平滑滚动不与引导4导航栏的工作
<body data-spy="scroll" data-target="#navbar" data-offset="20">
<!-- navbar -->
<div id="home">
<div id="navbar">
<nav class="navbar navbar-toggleable-md navbar-light bg-faded hidden-sm-down fixed-top mb-0">
<div class="container">
<a class="navbar-brand" href="#">honesty</a>
<div class="navbar-collapse justify-content-end" id="navCollapse">
<ul class="navbar-nav smooth-scroll">
<li class="nav-item">
<a class="nav-link smooth-scroll" href="#home">Home</a>
</li>
<li class="nav-item">
<a class="nav-link smooth-scroll" href="#about">About</a>
</li>
<li class="nav-item">
<a class="nav-link smooth-scroll" href="#portfolio">Portfolio</a>
</li>
<li class="nav-item">
<a class="nav-link smooth-scroll" href="#contact">Contact</a>
</li>
</ul>
</div>
</div>
</nav>
</div>
</div>
的scrollspy代码does not工作
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
// Add smooth scrolling to all links
$("a").on('click', function(event) {
// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {
// 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;
});
} // End if
});
});
</script>
(从W3Schools的网站得到的)
我真的不确定它是Bootstrap的导航栏导致问题还是我的jQuery或两者的组合。我将不胜感激任何帮助。我也听说scrollspy可能会混淆涉及导航栏和/或父元素的jQuery脚本。
请注意我用的引导4不3.
这打破了我的Scrollspy,它没有工作。这里是一个小提琴: –
你检查了codepen吗?它在那里滚动。所有你需要的是滚动到的ID。你有一个导航栏滚动到当前不存在的页面部分。您可以使用滚动所需的ID标记部分。另外,我没有看到你的小提琴链接。 –