下面是详细讲解“Bootstrap3.3.7导航栏下拉菜单鼠标滑过展开效果”的完整攻略:
准备工作
首先,你需要在HTML文件中引入Bootstrap库:
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
然后,在导航菜单中添加下拉菜单的HTML代码,如下所示:
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<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="#">Brand</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
<li><a href="#">Link</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></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 role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
实现鼠标滑过展开效果
接下来,你需要添加一些CSS代码来实现鼠标滑过下拉菜单展开的效果:
.dropdown:hover .dropdown-menu {
display: block;
}
这段CSS代码的意思是,当下拉菜单所在的 .dropdown
元素被鼠标滑过时,其子元素 .dropdown-menu
的 display
属性将变成 block
,从而展开下拉菜单。
示例说明
示例一
我们可以通过修改上述代码中的导航菜单,来看看实现效果。代码如下:
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<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="#">Brand</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home <span class="sr-only">(current)</span></a></li>
<li><a href="#">About</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Services <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Service one</a></li>
<li><a href="#">Service two</a></li>
<li><a href="#">Service three</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Service four</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Service five</a></li>
</ul>
</li>
<li><a href="#">Contact</a></li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
这里我们将导航菜单的内容修改为了一个网站导航。你可以在这个网站https://getbootstrap.com/docs/4.6/components/navbar/中找到这个完整的例子,并在此基础上修改。
当你运行这段代码并鼠标滑过 Services
时,你会发现下拉菜单展开了。
示例二
下面,我们来看一个稍微复杂一些的例子。代码如下:
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<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="#">Brand</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home <span class="sr-only">(current)</span></a></li>
<li><a href="#">About</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Products <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Product one</a></li>
<li><a href="#">Product two</a></li>
<li class="dropdown-submenu">
<a tabindex="-1" href="#">Product three <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a tabindex="-1" href="#">Product three one</a></li>
<li class="dropdown-submenu">
<a href="#">Product three two <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Product three two one</a></li>
<li><a href="#">Product three two two</a></li>
</ul>
</li>
<li><a tabindex="-1" href="#">Product three three</a></li>
</ul>
</li>
<li role="separator" class="divider"></li>
<li><a href="#">Product four</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Product five</a></li>
</ul>
</li>
<li><a href="#">Contact</a></li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
这里,我们添加了一个多级的下拉菜单。你可以在 Product three
中看到另一个下拉菜单。在这个例子中,你需要注意,当你鼠标滑过某个菜单项时,它的下一级菜单会展开,但在你离开它时,它的下一级菜单会收起,直到你又重复鼠标滑过该菜单项时才重新展开。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Bootstrap3.3.7导航栏下拉菜单鼠标滑过展开效果 - Python技术站