[英]How to make nav bar collapse only when in moble view
I am having issues with the nav bar collapsing when in full screen view. 在全屏视图中,导航栏崩溃了,我遇到了问题。 I want the nav bar to collapse in the hamburger via the mobile screen and i wrote a custom directive. 我想通过移动屏幕将导航栏折叠到汉堡包中,并编写了一个自定义指令。 Now when in full screen and you click a link the nav bar collapses and is causing an annoying flicker! 现在,在全屏模式下,当您单击链接时,导航栏会折叠并引起烦人的闪烁! Any ideas or help would be greatly appreciated! 任何想法或帮助将不胜感激! Here is my code: 这是我的代码:
js: JS:
.directive('collapseMenu', function () {
return {
link: function (scope, elem, attrs) {
$('.nav a').on('click', function(){
$('.navbar-toggle').click()
});
}
}
});
html: HTML:
<!-- Navigation -->
<nav class="navbar navbar-default" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div collapse-menu class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse-1">
<span class="sr-only">Show Menu</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!-- navbar-brand is hidden on larger screens, but visible when the menu is collapsed -->
<a class="navbar-brand" href="#/home"></a><a <img src="images/phone.png" alt="press to call"></a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="navbar-collapse-1" >
<ul class="nav navbar-nav">
<li><a class="home" href="#/home">Home</a></li>
<li><a class="about" href="#/about">About</a></li>
<li><a class="con" href="#/contact">Contact</a></li>
<li><a class="review" href="#/review">Reviews</a></li>
<li><a class="admin" href="#/admin">Admin</a></li>
</ul>
</div>
</div>
</nav>
So, if you want to eliminate the collapse on larger screens, you'll have to add a screen width check to your link click function. 因此,如果要消除较大屏幕上的折叠,您必须在链接点击功能中添加屏幕宽度检查。 And, if it's less than a certain size, you fire off the .navbar-toggle click function. 并且,如果它小于特定大小,则触发.navbar-toggle单击功能。 Otherwise, you do nothing. 否则,您什么也不做。 You could check the screen width using $(window).width()
. 您可以使用$(window).width()
检查屏幕宽度。 So, your directive code would become: 因此,您的指令代码将变为:
.directive('collapseMenu', function () {
return {
link: function (scope, elem, attrs) {
$('.nav a').on('click', function(){
if ($(window).width() <= 1200) {
$('.navbar-toggle').click();
}
});
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.