在 child 链接显示时突出显示 parent 导航项
Highlighting parent nav item when child links are showing
所以,我正在研究导航,它看起来像:
#head .nav {
position: relative;
z-index: 1;
}
#head .nav img {
float: left;
height: auto;
padding: 40px;
width: 200px;
}
#head .nav ul {
font-family: 'Roboto', sans-serif;
font-size: 16px;
padding-right: 20px;
text-align: center;
text-transform: uppercase;
transition: all 0.5s ease;
}
#head .nav li {
display: inline-block;
padding: 70px 0 20px;
position: relative;
}
#head .nav > ul > li > a {
color: #fff;
padding: 0 20px 10px;
}
#head .nav > ul > li > a:hover,
#head .nav > ul > li.current_page_ancestor > a {
border-bottom: 1px solid rgba(255, 231, 208, 0.5);
}
#head .nav li a {
text-decoration: none;
}
#head .nav li.current_page_item a {
border-bottom: 1px solid rgba(255, 231, 208, 0.5);
color: #ffe7d0;
}
#head .nav ul.children li {
padding: 0;
}
#head .nav ul.children li a {
background: rgba(255, 255, 255, 0.9);
color: #191a22;
display: block;
margin-bottom: 1px;
padding: 20px 20px 18px;
position: relative;
z-index: 1;
text-align: center;
width: 100px;
}
#head .nav ul.children li a:hover {
background: #fff;
}
#head .nav ul li ul {
display: none;
left: -50%;
margin-top: 20px;
padding: 0 0 0 10px;
position: absolute;
width: 100px;
}
#head .nav ul li:hover > ul,
#head .nav ul li ul:hover,
#head .nav ul li ul:focus {
display: block;
}
<div class="nav">
<a href=""><img src="https://url.com/images/logo.png" title="" alt=""></a>
<ul>
<li class="page_item page-item-7 current_page_item"><a href="https://url.com/" aria-current="page">Home</a></li>
<li class="page_item page-item-9"><a href="https://url.com/about/">About</a></li>
<li class="page_item page-item-11"><a href="https://url.com/media/">Media</a></li>
<li class="page_item page-item-13"><a href="https://url.com/music/">Music</a></li>
<li class="page_item page-item-15"><a href="https://url.com/shows/">Shows</a></li>
<li class="page_item page-item-23 page_item_has_children"><a href="https://url.com/shop/">Shop</a>
<ul class="children">
<li class="page_item page-item-24"><a href="https://url.com/shop/cart/">Cart</a></li>
<li class="page_item page-item-25"><a href="https://url.com/shop/checkout/">Checkout</a></li>
<li class="page_item page-item-26"><a href="https://url.com/shop/my-account/">My account</a></li>
</ul>
</li>
<li class="page_item page-item-20"><a href="https://url.com/contact/">Contact</a></li>
</ul>
</div>
我想做的是在显示 child 菜单时突出显示 parent...像这样:
我在想这里可能需要一些 JS,我试过了:
#head .nav > ul > li.page_item_has_children > a {
border-bottom: 1px solid rgba(255, 231, 208, 0.5);
}
就是一直把border-bottom
放在显示link上,不管child ul
是否显示,有没有办法添加border-bottom
仅当显示 #head .nav ul.children li a
时?
谢谢,
乔什
试试这个。
$(document).ready(function() {
$('.dropdown').click(function() {
$('.dropbtn').removeClass('dropbtn-active');
$(this).find('.dropbtn').addClass('dropbtn-active')
});
})
ul {
list-style-type: none;
padding-left: 1px;
}
.dropdown {
float: left;
padding-right: 1px;
}
.dropbtn {
height: 30px;
}
li a,
.dropbtn {
display: inline-block;
color: white;
text-align: center;
margin:10px;
padding:10px;
}
.dropbtn-active {
background-color: #31b0d5;
font-weight: bold;
color:black;
cursor:pointer;
}
li ,
.dropdown:hover .dropbtn {
background-color: #31b0d5;
}
.dropbtn:hover{
color:black;
cursor:pointer;
}
.dropdown a{
text-decoration:none;
}
.dropdown a:hover{
text-decoration:underline;
}
.dropdown:active .dropbtn {
background-color: #31b0d5;
}
a.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #e8f3f4;
min-width: 100%;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.1);
z-index: 10;
}
.dropdown-content a {
color: black;
padding: 5px 10px;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #d8dee2
}
.dropdown-content a:visited .dropbtn {
background-color: #e8f3f4;
}
.dropdown:hover .dropdown-content {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
<div id="menubar" class=" collapse navbar-collapse row">
<ul id="dropdownNavbar" class="navbar-nav">
<li class="dropdown">
<a class="btn dropbtn"><span class="glyphicon glyphicon-time"></span> Home
<span class="caret"></span>
</a>
</li>
<li class="dropdown">
<a class="btn dropbtn"><span class="glyphicon glyphicon-stats"></span> About
<span class="caret"></span>
</a>
</li>
<li class="dropdown">
<a class="btn dropbtn"><span class="glyphicon glyphicon-user"></span> Media
<span class="caret"></span>
</a>
</li>
<li class="dropdown">
<a class="btn dropbtn">
<span class="glyphicon glyphicon-briefcase"></span> Music
<span class="caret"></span>
</a>
</li>
<li class="dropdown">
<a class="btn dropbtn">
<span class="glyphicon glyphicon-headphones"></span> Shows
<span class="caret"></span>
</a>
</li>
<li class="dropdown">
<a class="btn dropbtn">
<span class="glyphicon glyphicon-file"></span>Shop
<span class="caret"></span>
</a>
<div class="dropdown-content">
<a href="#">Cart</a>
<a href="#">Checkout</a>
<a href="#">My Account</a>
</div>
</li>
<li class="dropdown">
<a class="btn dropbtn">
<span class="glyphicon glyphicon-cog"></span>Contact
<span class="caret"></span>
</a>
</li>
</ul>
</div>
</nav>
我重新开始,终于让一切正常,不需要 JS!
代码没有改变,这里是更新 CSS:
#head .nav {
position: relative;
z-index: 1;
}
#head .nav img {
height: auto;
padding: 40px;
position: absolute;
width: 200px;
z-index: 1;
}
#head .nav ul {
font-family: 'Roboto', sans-serif;
font-size: 0;
letter-spacing: 1px;
text-align: center;
text-transform: uppercase;
}
#head .nav li {
color: #fff;
display: inline-block;
font-size: 16px;
position: relative;
text-decoration: none;
}
#head .nav > ul {
padding-top: 50px;
}
#head .nav > ul > li > a {
border-bottom: 1px solid transparent;
display: inline-block;
padding: 20px 20px 10px;
}
#head .nav > ul > li:hover > a,
#head .nav > ul > li.current_page_item > a,
#head .nav > ul > li.current_page_ancestor > a {
border-bottom: 1px solid #ccc;
color: #ccc;
}
#head .nav li a {
color: #fff;
text-decoration: none;
}
#head .nav ul li ul {
border-top: 20px solid transparent;
display: none;
position: absolute;
left: 50%;
transform: translateX(-50%);
width: 200px;
}
#head .nav ul li:hover ul,
#head .nav ul li ul:hover,
#head .nav ul li ul:focus {
display: block;
}
#head .nav ul li ul li {
width: 100%;
}
#head .nav ul li ul li a {
background: rgba(255, 255, 255, 0.9);
color: #5d6896;
box-sizing: border-box;
display: block;
margin-bottom: 1px;
padding: 20px 20px 18px;
}
#head .nav ul li ul li a:hover {
background: #fff;
color: #191a22;
}
真正解决我问题的那个:
#head .nav > ul > li:hover > a
现在效果好多了,我喜欢它的结果,希望这对其他人有帮助!
谢谢,
乔什
所以,我正在研究导航,它看起来像:
#head .nav {
position: relative;
z-index: 1;
}
#head .nav img {
float: left;
height: auto;
padding: 40px;
width: 200px;
}
#head .nav ul {
font-family: 'Roboto', sans-serif;
font-size: 16px;
padding-right: 20px;
text-align: center;
text-transform: uppercase;
transition: all 0.5s ease;
}
#head .nav li {
display: inline-block;
padding: 70px 0 20px;
position: relative;
}
#head .nav > ul > li > a {
color: #fff;
padding: 0 20px 10px;
}
#head .nav > ul > li > a:hover,
#head .nav > ul > li.current_page_ancestor > a {
border-bottom: 1px solid rgba(255, 231, 208, 0.5);
}
#head .nav li a {
text-decoration: none;
}
#head .nav li.current_page_item a {
border-bottom: 1px solid rgba(255, 231, 208, 0.5);
color: #ffe7d0;
}
#head .nav ul.children li {
padding: 0;
}
#head .nav ul.children li a {
background: rgba(255, 255, 255, 0.9);
color: #191a22;
display: block;
margin-bottom: 1px;
padding: 20px 20px 18px;
position: relative;
z-index: 1;
text-align: center;
width: 100px;
}
#head .nav ul.children li a:hover {
background: #fff;
}
#head .nav ul li ul {
display: none;
left: -50%;
margin-top: 20px;
padding: 0 0 0 10px;
position: absolute;
width: 100px;
}
#head .nav ul li:hover > ul,
#head .nav ul li ul:hover,
#head .nav ul li ul:focus {
display: block;
}
<div class="nav">
<a href=""><img src="https://url.com/images/logo.png" title="" alt=""></a>
<ul>
<li class="page_item page-item-7 current_page_item"><a href="https://url.com/" aria-current="page">Home</a></li>
<li class="page_item page-item-9"><a href="https://url.com/about/">About</a></li>
<li class="page_item page-item-11"><a href="https://url.com/media/">Media</a></li>
<li class="page_item page-item-13"><a href="https://url.com/music/">Music</a></li>
<li class="page_item page-item-15"><a href="https://url.com/shows/">Shows</a></li>
<li class="page_item page-item-23 page_item_has_children"><a href="https://url.com/shop/">Shop</a>
<ul class="children">
<li class="page_item page-item-24"><a href="https://url.com/shop/cart/">Cart</a></li>
<li class="page_item page-item-25"><a href="https://url.com/shop/checkout/">Checkout</a></li>
<li class="page_item page-item-26"><a href="https://url.com/shop/my-account/">My account</a></li>
</ul>
</li>
<li class="page_item page-item-20"><a href="https://url.com/contact/">Contact</a></li>
</ul>
</div>
我想做的是在显示 child 菜单时突出显示 parent...像这样:
我在想这里可能需要一些 JS,我试过了:
#head .nav > ul > li.page_item_has_children > a {
border-bottom: 1px solid rgba(255, 231, 208, 0.5);
}
就是一直把border-bottom
放在显示link上,不管child ul
是否显示,有没有办法添加border-bottom
仅当显示 #head .nav ul.children li a
时?
谢谢,
乔什
试试这个。
$(document).ready(function() {
$('.dropdown').click(function() {
$('.dropbtn').removeClass('dropbtn-active');
$(this).find('.dropbtn').addClass('dropbtn-active')
});
})
ul {
list-style-type: none;
padding-left: 1px;
}
.dropdown {
float: left;
padding-right: 1px;
}
.dropbtn {
height: 30px;
}
li a,
.dropbtn {
display: inline-block;
color: white;
text-align: center;
margin:10px;
padding:10px;
}
.dropbtn-active {
background-color: #31b0d5;
font-weight: bold;
color:black;
cursor:pointer;
}
li ,
.dropdown:hover .dropbtn {
background-color: #31b0d5;
}
.dropbtn:hover{
color:black;
cursor:pointer;
}
.dropdown a{
text-decoration:none;
}
.dropdown a:hover{
text-decoration:underline;
}
.dropdown:active .dropbtn {
background-color: #31b0d5;
}
a.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #e8f3f4;
min-width: 100%;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.1);
z-index: 10;
}
.dropdown-content a {
color: black;
padding: 5px 10px;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #d8dee2
}
.dropdown-content a:visited .dropbtn {
background-color: #e8f3f4;
}
.dropdown:hover .dropdown-content {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
<div id="menubar" class=" collapse navbar-collapse row">
<ul id="dropdownNavbar" class="navbar-nav">
<li class="dropdown">
<a class="btn dropbtn"><span class="glyphicon glyphicon-time"></span> Home
<span class="caret"></span>
</a>
</li>
<li class="dropdown">
<a class="btn dropbtn"><span class="glyphicon glyphicon-stats"></span> About
<span class="caret"></span>
</a>
</li>
<li class="dropdown">
<a class="btn dropbtn"><span class="glyphicon glyphicon-user"></span> Media
<span class="caret"></span>
</a>
</li>
<li class="dropdown">
<a class="btn dropbtn">
<span class="glyphicon glyphicon-briefcase"></span> Music
<span class="caret"></span>
</a>
</li>
<li class="dropdown">
<a class="btn dropbtn">
<span class="glyphicon glyphicon-headphones"></span> Shows
<span class="caret"></span>
</a>
</li>
<li class="dropdown">
<a class="btn dropbtn">
<span class="glyphicon glyphicon-file"></span>Shop
<span class="caret"></span>
</a>
<div class="dropdown-content">
<a href="#">Cart</a>
<a href="#">Checkout</a>
<a href="#">My Account</a>
</div>
</li>
<li class="dropdown">
<a class="btn dropbtn">
<span class="glyphicon glyphicon-cog"></span>Contact
<span class="caret"></span>
</a>
</li>
</ul>
</div>
</nav>
我重新开始,终于让一切正常,不需要 JS!
代码没有改变,这里是更新 CSS:
#head .nav {
position: relative;
z-index: 1;
}
#head .nav img {
height: auto;
padding: 40px;
position: absolute;
width: 200px;
z-index: 1;
}
#head .nav ul {
font-family: 'Roboto', sans-serif;
font-size: 0;
letter-spacing: 1px;
text-align: center;
text-transform: uppercase;
}
#head .nav li {
color: #fff;
display: inline-block;
font-size: 16px;
position: relative;
text-decoration: none;
}
#head .nav > ul {
padding-top: 50px;
}
#head .nav > ul > li > a {
border-bottom: 1px solid transparent;
display: inline-block;
padding: 20px 20px 10px;
}
#head .nav > ul > li:hover > a,
#head .nav > ul > li.current_page_item > a,
#head .nav > ul > li.current_page_ancestor > a {
border-bottom: 1px solid #ccc;
color: #ccc;
}
#head .nav li a {
color: #fff;
text-decoration: none;
}
#head .nav ul li ul {
border-top: 20px solid transparent;
display: none;
position: absolute;
left: 50%;
transform: translateX(-50%);
width: 200px;
}
#head .nav ul li:hover ul,
#head .nav ul li ul:hover,
#head .nav ul li ul:focus {
display: block;
}
#head .nav ul li ul li {
width: 100%;
}
#head .nav ul li ul li a {
background: rgba(255, 255, 255, 0.9);
color: #5d6896;
box-sizing: border-box;
display: block;
margin-bottom: 1px;
padding: 20px 20px 18px;
}
#head .nav ul li ul li a:hover {
background: #fff;
color: #191a22;
}
真正解决我问题的那个:
#head .nav > ul > li:hover > a
现在效果好多了,我喜欢它的结果,希望这对其他人有帮助!
谢谢,
乔什