gpt4 book ai didi

javascript - Bootstrap 滚动 spy 不工作和导航菜单按钮导致标题重新加载

转载 作者:行者123 更新时间:2023-12-03 07:39:27 25 4
gpt4 key购买 nike

基本上所有 anchor href 都可以工作,除了滚动 spy 不起作用。数据偏移也不起作用。单击时,只有第一个li“Home”正在更改其背景,所有其他都不起作用。向下滚动到 #search-nav #obqvi #about 等与它们相关的菜单按钮也不会变色,这意味着我的滚动 spy 根本不工作。

此外,当我单击其中一个 anchor 时,整个标题菜单按钮会重新加载,它会发生大约 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>

最佳答案

<!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>

关于javascript - Bootstrap 滚动 spy 不工作和导航菜单按钮导致标题重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35469380/

25 4 0
文章推荐: javascript - JSX:
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com