When we open collapse, button change color. but when we closed collapse by button click, the button doesn't return to its initial color. Button is focused. And I want that lost the focus.
I have tried with css :focus, but it didn´t lose focus after collapse have been closed by button. I think that jquery function is necessary, although I've tried, my js skills shouldn't be sufficient.
I need help please
Demo
My Html code:
<div class="wrapper">
<div class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Project name</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li class="divider"></li>
<li class="dropdown-header">Nav header</li>
<li><a href="#">Separated link</a></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="active"><a href="./">Default</a></li>
<li><a href="../navbar-static-top/">Static top</a></li>
<li><a href="../navbar-fixed-top/">Fixed top</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
My CSS code:
.wrapper {width:100%, height:100%;}
My jQuery function:
$(document).ready(function () {
function CloseNav() {
$(".navbar-collapse").stop().animate({'height': 0},300, function () {
$('.navbar-collapse').removeClass('in').addClass("collapse");
});
$(".navbar-toggle").stop().removeClass('collapsed');
}
$('html').click(function (event) {
var clickover = $(event.target);
var _opened = $(".navbar-collapse").hasClass("navbar-collapse in");
if (_opened === true && !clickover.hasClass("navbar-toggle")) {
CloseNav();
}
});
});
Hi the problem here is with the focus
state of the button once you click it never loses the focus unless you click outside again. You can try this:
First make a class that replicates the hover
and focus
state for the button:
.highlight {background:#ddd}
Then you can modify your Jquery to force the button lose his focus state on click
and apply the class to keep the visual effect:
$('.navbar-toggle').click(function() {
$(this).toggleClass('highlight').blur();
});
Also since your collapse close every time you click outside the button you need to add some there:
if (_opened === true && !clickover.hasClass("navbar-toggle")) {
$('.navbar-toggle').toggleClass('highlight'); /**Add This**/
CloseNav();
}
Check this Demo Fiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With