如何在下拉菜单中保持对父项的焦点状态?

时间:2018-09-20 16:21:54

标签: html css

我有一个带有下拉菜单的菜单。当我单击下拉菜单时,子菜单是一种颜色。单击菜单后,我试图在父项中保持相同的颜色(处于悬停状态)。当前,当我单击下拉列表,然后将鼠标从父级移开时,它会变回原始菜单颜色(粉红色)。

最好在整个页面中打开该代码段,以查看我的意思。

.dropdown-menu {
	border: none;
	border-radius: 0;
	margin: 0;
}

.dropdown-item:hover {
	background-color: #0091c7;
}

.nav-link.dropdown-toggle:hover {
	background-color: #00aeef;
}


.nav-link > li.dropdown-toggle.active > a,
.nav-link > li.dropdown-toggle.active > a:focus,
.nav-link > li.dropdown-toggle.active > a:hover {
	background-color: #00aeef;
	color: #ffffff;
}

.bg-pink {
	background-color: #d60c8c;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.bundle.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container p-5">
  <div class="row">
    <div class="col-sm-12">
    <!-- HTML here -->
    <div class="container-fluid bg-pink">
      <div class="container">
        <div class="row">
          <div class="col-lg-12">
            <nav class="navbar navbar-expand-lg navbar-light bg-pink">
              <a class="navbar-brand" href="#">
              <i class="fa fa-home fa-2x"></i>
              </a>
              <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
              <span class="navbar-toggler-icon"></span>
              </button>

              <div class="collapse navbar-collapse" id="navbarSupportedContent">
              <ul class="navbar-nav mr-auto">
                <li class="nav-item">
                  <a class="nav-link" href="#">Link</a>
                </li>							  
                <li class="nav-item">
                  <a class="nav-link" href="#">Link</a>
                </li>							  
                <li class="nav-item">
                  <a class="nav-link" href="#">Link</a>
                </li>							  
                <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  Dropdown Parent
                </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdown" style="background-color: #00aeef;">
                  <a class="dropdown-item" href="#">Action</a>
                  <div class="dropdown-divider"></div>
                  <a class="dropdown-item" href="#">Action</a>
                  <a class="dropdown-item" href="#">Action Action</a>
                  <a class="dropdown-item" href="#">Action Action</a>
                  <a class="dropdown-item" href="#">Action Action</a>
                  <a class="dropdown-item" href="#">Action Action Action</a>
                </div>
                </li>						  
                <li class="nav-item">
                  <a class="nav-link" href="#">Link</a>
                </li>							  
                <li class="nav-item">
                  <a class="nav-link" href="#">Link</a>
                </li>							  
                <li class="nav-item">
                  <a class="nav-link" href="#">Link</a>
                </li>							  
              </ul>
              </div>
            </nav>		
          </div>
        </div>
      </div>
    </div>  
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

单击下拉列表后,show类将添加到其父级li

我添加了

li.show > #navbarDropdown,

到您的CSS此处:

li.show > #navbarDropdown,
.nav-link > li.dropdown-toggle.active > a,
.nav-link > li.dropdown-toggle.active > a:focus,
.nav-link > li.dropdown-toggle.active > a:hover {
    background-color: #00aeef;
    color: #ffffff;
}