JavaScript - Bootstrap 4 - 仅在折叠时隐藏导航栏
Posted
技术标签:
【中文标题】JavaScript - Bootstrap 4 - 仅在折叠时隐藏导航栏【英文标题】:JavaScript - Bootstrap 4 - hide navbar only when it's collapsed 【发布时间】:2019-03-11 12:15:25 【问题描述】:我不希望导航栏在未折叠时隐藏。如果你展开导航栏然后向下滚动,你会看到导航栏仍然隐藏,这让导航栏有点奇怪。如何解决?
var prevScrollpos = window.pageYOffset;
window.onscroll = function()
var currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos)
document.getElementById("navbarHide").style.top = "0";
else
document.getElementById("navbarHide").style.top = "-70px";
prevScrollpos = currentScrollPos;
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body style="height: 1000px">
<nav class="navbar navbar-light bg-secondary sticky-top" id="navbarHide">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo02">
<ul class="navbar-nav mr-auto mt-2 mt-lg-0">
<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>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
【问题讨论】:
你希望当你往下走时菜单在顶部可见吗? @DavidLeonardoMolinaRuizDav 是的,但仅限于未折叠时。 【参考方案1】:嗯,我做的第一件事是创建一个带有 $(".navbar-toggler")
类的点击事件,然后我评估菜单是否折叠,如果它折叠我添加 fixed-top
类,如果我不删除它.我希望它是您正在寻找的:
$(".navbar-toggler").on("click",function()
if($("#navbarTogglerDemo02").hasClass("show"))
$(".navbar").removeClass("fixed-top");
else
$(".navbar").addClass("fixed-top");
);
var lastScrollTop = 0;
$(window).scroll(function(event)
var st = $(this).scrollTop();
if (st < lastScrollTop && !$("#navbarTogglerDemo02").hasClass("show"))
$(".navbar").addClass("fixed-top");
else if(st > lastScrollTop && !$("#navbarTogglerDemo02").hasClass("show"))
$(".navbar").removeClass("fixed-top");
lastScrollTop = st;
);
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body style="height: 1000px">
<nav class="navbar navbar-light bg-secondary" id="navbarHide">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo02">
<ul class="navbar-nav mr-auto mt-2 mt-lg-0">
<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>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
【讨论】:
其实还剩下一件事,当我向上滚动时菜单应该是可见的,但只有在它折叠时才可见。这可能吗? 准备好了,现在可以查看了以上是关于JavaScript - Bootstrap 4 - 仅在折叠时隐藏导航栏的主要内容,如果未能解决你的问题,请参考以下文章
用于bootstrap 4网格系统的可视化javascript编辑器,作为jQuery插件编写。
JavaScript 不适用于 bootstrap 4 + thymeleaf
javascript 顺畅滚动jQuery Bootstrap 4
javascript 带滚动间谍的Bootstrap 4导航栏