简体   繁体   English

在 CSS 中防止父动画到子动画

[英]Prevent parent animation to child in CSS

 nav { display: inline-flex; align-items: center; width: 100%; height: 8rem; border-bottom: 1px solid black; }.nav-list { display: flex; width: 100%; }.nav-list li { line-height: 8rem; position: relative; }.sub-menu li { color: #c40707; line-height: 7rem; }.nav-list a { display: block; color: black; padding: 0 1.5rem; font-size: 1.4rem; text-transform: uppercase; transition: color 300ms; }.nav-list a::after { content: ""; position: absolute; background-color: #ff2a00; height: 3px; width: 0; left: 0; bottom: 15px; transition: 0s; }.nav-list a:hover::after { width: 100%; }.nav-list a:hover { color: #e3dedd; }.sub-menu a { color: #7e7978; font-size: 1.5rem; font-weight: 200; }.sub-menu { width: 20rem; display: block; position: absolute; visibility: hidden; z-index: 500; background-color: rgb(255, 255, 255); box-sizing: border-box; top: 2rem; }.nav-list li:hover>.sub-menu { top: 7.5rem; opacity: 1; visibility: visible; }
 <header> <ul class="nav-list"> <li> <a href="%">Men</a> <ul class="sub-menu"> <li><a href="#">shirts</a></li> <li><a href="#">Shorts</a></li> <li><a href="#">Tracks</a></li> <li><a href="#">Shoes</a></li> </ul> </li> </ul> </header>

Im trying to create a dropdown using CSS.我正在尝试使用 CSS 创建下拉菜单。 I created a nav bar with certain elements that are mentioned in the code.我用代码中提到的某些元素创建了一个导航栏。 I applied a hover effect to the nav bar.我对导航栏应用了悬停效果。 I created a dropdown using .nav-list li: hover >.sub-menu as the command.我使用.nav-list li: hover >.sub-menu作为命令。 However, the hover effect I had for the nav bar is being applied to the sub menu too.但是,我对导航栏的悬停效果也应用于子菜单。 How to prevent this from happening?如何防止这种情况发生?

Add the class "main-menu-item"添加类“主菜单项”

<a class="main-menu-item" href="%">Men</a>

In the CSS Code, change your hover and after effect from "a" to this:在 CSS 代码中,将悬停和后效果从“a”更改为:

.nav-list .main-menu-item::after {
        content: "";
        position: absolute;
        background-color: #ff2a00;
        height: 3px;
        width: 0;
        left: 0;
        bottom: 15px;
        transition: 0s;
    }

.nav-list .main-menu-item:hover {
        color: #e3dedd;
    }

Here is the running example:这是运行示例:

 nav { display: inline-flex; align-items: center; width: 100%; height: 8rem; border-bottom: 1px solid black; }.nav-list { display: flex; width: 100%; }.nav-list li { line-height: 8rem; position: relative; }.sub-menu li { color: #c40707; line-height: 7rem; }.nav-list a { display: block; color: black; padding: 0 1.5rem; font-size: 1.4rem; text-transform: uppercase; transition: color 300ms; }.nav-list.main-menu-item::after { content: ""; position: absolute; background-color: #ff2a00; height: 3px; width: 0; left: 0; bottom: 15px; transition: 0s; }.nav-list a:hover::after { width: 100%; }.nav-list.main-menu-item:hover { color: #e3dedd; }.sub-menu a { color: #7e7978; font-size: 1.5rem; font-weight: 200; }.sub-menu { width: 20rem; display: block; position: absolute; visibility: hidden; z-index: 500; background-color: rgb(255, 255, 255); box-sizing: border-box; top: 2rem; }.nav-list li:hover>.sub-menu { top: 7.5rem; opacity: 1; visibility: visible; }
 <header> <div class="container"> <nav> </div> <ul class="nav-list"> <li> <a class="main-menu-item" href="%">Men</a> <ul class="sub-menu"> <li> <a href="#">shirts</a> </li> <li> <a href="#">Shorts</a> </li> <li> <a href="#">Tracks</a> </li> <li> <a href="#">Shoes</a> </li> </ul> </li> </ul> </header>

The problem is that you need to be more specific in your CSS selectors.问题是您需要在 CSS 选择器中更加具体。 Since you only want the hover to affect the "Men" top level parent element, you need to use the direct descendent > selector after your .nav-list selector - since you only want the top level li a .由于您只希望hover影响“Men”顶级父元素,因此您需要在.nav-list选择器之后使用直接后代>选择器 - 因为您只需要顶级li a

I changed your CSS in this part:我在这部分更改了您的 CSS:

.nav-list > li > a {
  display: block;
  color: black;
  padding: 0 1.5rem;
  font-size: 1.4rem;
  text-transform: uppercase;
  transition: color 300ms;
}

.nav-list > li > a::after {
  content: "";
  position: absolute;
  background-color: #ff2a00;
  height: 3px;
  width: 0;
  left: 0;
  bottom: 15px;
  transition: 0s;
}

.nav-list > li > a:hover::after {
  width: 100%;
}

That ensures that only the top level direct children elements li and a are selected.这确保选择顶级直接子元素lia

 nav { display: inline-flex; align-items: center; width: 100%; height: 8rem; border-bottom: 1px solid black; }.nav-list { display: flex; width: 100%; }.nav-list li { line-height: 8rem; position: relative; }.sub-menu li { color: #c40707; line-height: 7rem; }.nav-list > li > a { display: block; color: black; padding: 0 1.5rem; font-size: 1.4rem; text-transform: uppercase; transition: color 300ms; }.nav-list > li > a::after { content: ""; position: absolute; background-color: #ff2a00; height: 3px; width: 0; left: 0; bottom: 15px; transition: 0s; }.nav-list > li > a:hover::after { width: 100%; }.nav-list a:hover { color: #e3dedd; }.sub-menu a { color: #7e7978; font-size: 1.5rem; font-weight: 200; }.sub-menu { width: 20rem; display: block; position: absolute; visibility: hidden; z-index: 500; background-color: rgb(255, 255, 255); box-sizing: border-box; top: 2rem; }.nav-list li:hover >.sub-menu { top: 7.5rem; opacity: 1; visibility: visible; }
 <header> <ul class="nav-list"> <li> <a href="%">Men</a> <ul class="sub-menu"> <li><a href="#">shirts</a></li> <li><a href="#">Shorts</a></li> <li><a href="#">Tracks</a></li> <li><a href="#">Shoes</a></li> </ul> </li> </ul> </header>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM