twitter-bootstrap 如何我的切换按钮更改颜色只有我的折叠在我的引导程序 3 导航栏中打开
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21334226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to my toggle button change color only my collapse is opened in my bootstrap 3 navbar
提问by user3216077
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.
我已经尝试过使用 css :focus,但是在按钮关闭折叠后它并没有失去焦点。我认为jquery功能是必须的,虽然我试过了,但我的js技能应该不够。
I need help please
我需要帮助
My Html code:
我的 HTML 代码:
<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:
我的 CSS 代码:
.wrapper {width:100%, height:100%;}
My jQuery function:
我的 jQuery 函数:
$(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();
}
});
});
采纳答案by DaniP
Hi the problem here is with the focusstate of the button once you click it never loses the focus unless you click outside again. You can try this:
嗨,这里的问题是focus按钮的状态,一旦你点击它就永远不会失去焦点,除非你再次点击外面。你可以试试这个:
First make a class that replicates the hoverand focusstate for the button:
首先创建一个复制按钮hover和focus状态的类:
.highlight {background:#ddd}
Then you can modify your Jquery to force the button lose his focus state on clickand apply the class to keep the visual effect:
然后您可以修改您的 Jquery 以强制按钮失去焦点状态click并应用该类以保持视觉效果:
$('.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
检查这个演示小提琴
回答by hsobhy
Try this CSS ... a JS Fiddle
试试这个 CSS ... 一个JS Fiddle
.navbar-default button.navbar-toggle.collapsed:focus,
.navbar-default button.navbar-toggle.collapsed{ background-color: transparent;
-webkit-transition:all .4s;
-moz-transition:all .4s;
-ms-transition:all .4s;
-o-transition:all .4s;
transition:all .4s;
}
.navbar-default button.navbar-toggle:focus,
.navbar-default button.navbar-toggle.collapsed:hover{ background-color:#ddd;
-webkit-transition:all .4s;
-moz-transition:all .4s;
-ms-transition:all .4s;
-o-transition:all .4s;
transition:all .4s;
}

