gpt4 book ai didi

css - Bootstrap 下拉菜单不支持悬停时的单个元素

转载 作者:行者123 更新时间:2023-11-28 15:04:08 25 4
gpt4 key购买 nike

在 Bootstrap 导航栏中实现下拉菜单后,出于某种原因,下划线动画不会在下拉列表中的各个元素下划线 - 它只会在最底部下划线。应该如何解决这个问题?

.navbar>navbar-brand {
color: black;
}

.navbar-default .navbar-nav>li>a {
color: black;
}

.navbar-default .navbar-nav>li>a:active,
.navbar-default .navbar-nav>li>a:focus {
background-color: black;
color: white;
}

.navbar-default ul li a:hover {
color: #000;
}

.navbar-default ul li a:hover:before {
width: 100%;
}

.navbar-default ul li a:before {
content: '';
display: block;
position: absolute;
left: 0;
bottom: 0;
height: 3px;
width: 0;
background: #000;
-webkit-transition: width .25s;
-moz-transition: width .25s;
-ms-transition: width .25s;
-o-transition: width .25s;
transition: width .25s;
}

.navbar-default ul li.last>a:after,
#cssmenu ul li:last-child>a:after {
display: none;
}

@media screen and (max-width: 768px) {
.navbar-default ul li {
float: none;
width: 100%;
}
.navbar-default ul li a {
width: 100%;
line-height: 2em;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
.navbar-default ul li a:after {
display: none;
}
.navbar-default ul li a:before {
height: 1px;
background: #000;
width: 100%;
opacity: .2;
}
.navbar-default ul li.last>a:before,
.navbar-default ul li:last-child>a:before {
display: none;
}
}

@media screen and (min-width: 768px) {
#myNavbar ul:not(.dropdown-menu) {
display: flex;
flex-wrap: wrap;
zoom: 1;
}
#myNavbar ul li {
flex-grow: 1;
}
#myNavbar ul li a {
text-align: left;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<nav class="navbar navbar-default">
<div class="container-fluid">

<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<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="#">Hello World</a>
</div>

<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav flex-row">
<li><a href="#">Link A</a></li>
<li><a href="#">Link B</a></li>
<li><a href="#">Link C</a></li>
<li><a href="#">Link D</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Link E <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Link E.1</a></li>
<li><a href="#">Link E.2</a></li>
<li><a href="#">Link E.3</a></li>
<li><a href="#">Link E.4</a></li>
</ul>
</li>
<li><a href="#">Link F</a></li>
<li><a href="#">Link G</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Link H <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Link H.1</a></li>
<li><a href="#">Link H.2</a></li>
</ul>
</li>
<li><a href="#">Link I</a></li>
<li><a href="#">Link J</a></li>
</ul>
</div>
</div>
</nav>

最佳答案

目前动画绝对定位到<ul>元素。所以它只在 <ul> 的底部可见元素 ( bottom:0 )。你需要设置 position: relative<li><a>元素,以便动画绝对定位到菜单项。

因此您可以添加以下 CSS 来解决问题:

.navbar-default ul li a {
position:relative;
}

参见以下解决方案:

.navbar>navbar-brand {
color: black;
}

.navbar-default .navbar-nav>li>a {
color: black;
}

.navbar-default .navbar-nav>li>a:active,
.navbar-default .navbar-nav>li>a:focus {
background-color: black;
color: white;
}
.navbar-default ul li a {
position:relative;
}
.navbar-default ul li a:hover {
color: #000;
}

.navbar-default ul li a:hover:before {
width: 100%;
}

.navbar-default ul li a:before {
content: '';
display: block;
position: absolute;
left: 0;
bottom:0;
height: 3px;
width: 0;
background: #000;
-webkit-transition: width .25s;
-moz-transition: width .25s;
-ms-transition: width .25s;
-o-transition: width .25s;
transition: width .25s;
}

.navbar-default ul li.last>a:after,
#cssmenu ul li:last-child>a:after {
display: none;
}

@media screen and (max-width: 768px) {
.navbar-default ul li {
float: none;
width: 100%;
}
.navbar-default ul li a {
width: 100%;
line-height: 2em;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
.navbar-default ul li a:after {
display: none;
}
.navbar-default ul li a:before {
height: 1px;
background: #000;
width: 100%;
opacity: .2;
}
.navbar-default ul li.last>a:before,
.navbar-default ul li:last-child>a:before {
display: none;
}
}

@media screen and (min-width: 768px) {
#myNavbar ul:not(.dropdown-menu) {
display: flex;
flex-wrap: wrap;
zoom: 1;
}
#myNavbar ul li {
flex-grow: 1;
}
#myNavbar ul li a {
text-align: left;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<nav class="navbar navbar-default">
<div class="container-fluid">

<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#myNavbar" aria-expanded="false">
<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="#">Hello World</a>
</div>

<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav flex-row">
<li><a href="#">Link A</a></li>
<li><a href="#">Link B</a></li>
<li><a href="#">Link C</a></li>
<li><a href="#">Link D</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Link E <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Link E.1</a></li>
<li><a href="#">Link E.2</a></li>
<li><a href="#">Link E.3</a></li>
<li><a href="#">Link E.4</a></li>
</ul>
</li>
<li><a href="#">Link F</a></li>
<li><a href="#">Link G</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Link H <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Link H.1</a></li>
<li><a href="#">Link H.2</a></li>
</ul>
</li>
<li><a href="#">Link I</a></li>
<li><a href="#">Link J</a></li>
</ul>
</div>
</div>
</nav>

关于css - Bootstrap 下拉菜单不支持悬停时的单个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49798988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com