因此,当我刚刚将示例中的代码复制并粘贴到HTML文件中的脚本标签中时,它工作得很好。当我开始向下滚动时,导航栏最终会停留在顶部。
但是我想外部使用JS代码。因此,我制作了一个外部文件,添加了eventlisteners等。为了测试这些额外的功能是否起作用,我在其中添加了一些console.logs。控制台向我显示实际上一切正常。 除了事实之外,导航栏不再具有粘性。
我已经问过Google,并阅读了很多其他有关粘性导航栏的问题。只是没有帮助。
希望任何人都知道解决方案-怎么了,为什么它不起作用,我该怎么做才能解决?
var navbar = document.getElementById("navbar");
var sticky = navbar.offsetTop;
function stickyNavbar() {
if (window.pageYOffset >= sticky) {
navbar.classList.add("sticky");
} else {
navbar.classList.remove("sticky");
}
console.log(navbar.classList);
}
function init() {
document.addEventListener('scroll', stickyNavbar());
console.log("init-function is successfully connected with document.addEventListener ")
}
document.addEventListener('load', init(), false);
document.addEventListener('load', console.log("page has loaded successfully "), false);
.sticky {
position: fixed;
top: 0;
width: 100%;
}
h1 {
color: red;
}
header {
transform: none;
}
.top-container {
background-color: #f1f1f1;
padding: 30px;
text-align: center;
}
#navbar {
padding: 10px 16px;
background: #555;
color: #f1f1f1;
width: 100%;
}
.sticky+.content {
padding-top: 102px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<header>
<section class="container top-container">
<h1>Innere Kinder</h1>
</section>
<!-- Navigation -->
<nav id="navbar" class="navbar navbar-expand-lg navbar-light" style="background-color: black">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
</header>
<body class="content">
<section class="container">
"Some example text that would have to be extended to actually show the sticky navbar feature"
</section>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>