有没有办法通过JS找出切换按钮菜单是否已折叠或隐藏。
class =“navbar-toggle” 或data-toggle =“collapse”是否有指示此状态的参数?
我知道显示或隐藏“.collape”时调用的事件:
$('.navbar-collapse').on('shown.bs.collapse', function () {
//called when dropdown menu is shown
});
此外,我知道显示或隐藏崩溃的方法如下:
$('.navbar-collapse').collapse('hide');
但我找不到有关如何获取.collapse当前状态的任何信息,告诉我它是隐藏还是显示。
答案 0 :(得分:8)
(我正在以更好的方式编辑这个答案。)
是的,您可以使用jquery $(element).is(“:visible”)检查元素是否可见
这是一些html
background: repeating-linear-gradient(-45deg, #de9dd4, #de9dd4 5px, white 5px, white 10px);
这是一些javascript
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">WebSiteName</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Page 1 <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Page 1-1</a></li>
<li><a href="#">Page 1-2</a></li>
<li><a href="#">Page 1-3</a></li>
</ul>
</li>
<li><a href="#">Page 2</a></li>
<li><a href="#">Page 3</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="#"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
<li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
</ul>
</div>
</div>
</nav>
<div class="container">
<h3>Collapsible Navbar</h3>
<p>In this example, the navigation bar is hidden on small screens and replaced by a button in the top right corner (try to re-size this window).
<p>Only when the button is clicked, the navigation bar will be displayed.</p>
</div>
<input type="button" value="click me" onclick="clickedIt()" />