所以即使现在我也不能简单地解释我的问题。我有简单的两步菜单。当您悬停在菜单的第一步时,悬停元素更改颜色并显示菜单的第二步。还有我的问题: 当我将鼠标悬停在第一步的菜单元素的第二步(这是你将其悬停以显示第二步的那一步)时将颜色更改为默认值,就像悬停在第一步上永远不会发生一样。 例如:第一步的一个元素称为ipsum。当我将鼠标悬停在" ipsum"出现第二步菜单" ipsum"改变它的颜色。当我将鼠标悬停在刚刚出现的第二步菜单上时," ipsum"将颜色更改为菜单的默认颜色。 我试图发现它为什么会发生,但我不知道。
*{
padding: 0;
margin: 0;
}
/*main menu*/
.menu {
list-style-type: none;
text-align: center;
text-transform: uppercase;
font-family: "Courier New", Courier, monospace;
background-color: lightgrey;
}
/*all links in menu*/
.menu a {
text-decoration: none;
color: #404040;
font-weight: 700;
transition: background-color 0.8s;
}
.menu > li {
display: inline-block;
font-size: 1.5em;
}
.menu > li > a {
display: block;
padding: 10px 12px;
}
.menu > li > a:hover {
background-color: #ababab;
}
/*second step menus*/
.sStep {
position: absolute;
/*to nice 'fade in'*/
visibility: hidden;
opacity: 0;
transition: visibility 0.5s, opacity 0.5s linear;
list-style-type: none;
background-color: #ababab;
}
.sStep > li {
font-size: 0.75em;
border-bottom: 1px solid #444;
}
.sStep > li:last-child {
border: none;
}
.sStep > li > a {
display: block;
padding: 10px;
}
.sStep > li > a:hover {
background-color: #909090;
}
/*'fade in hover' */
.menu > li:hover > .sStep {
visibility: visible;
opacity: 1;
}

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>menu2</title>
<link rel="stylesheet" type="text/css" href="style2.css" />
</head>
<body>
<nav>
<ul class="menu">
<li><a href="#">Lorem</a></li>
<li><a href="#">ipsum</a>
<ul class="sStep">
<li><a href="#">Suspendisse</a></li>
<li><a href="#">Donec commodo</a></li>
<li><a href="#">sodales consectetur</a></li>
<li><a href="#">Maecenas sit amet</a></li>
<li><a href="#">Mauris tempor vitae sem</a></li>
<li><a href="#">Curabitur</a></li>
</ul>
</li>
<li><a href="#">dolor</a></li>
<li><a href="#">sit</a></li>
<li><a href="#">amet</a>
<ul class="sStep">
<li><a href="#">ullamcorper cursus</a></li>
<li><a href="#">Cras blandit nunc</a></li>
<li><a href="#">fermentum</a></li>
<li><a href="#">Neque porro quisquam</a></li>
</ul>
</li>
</ul>
</nav>
</body>
</html>
&#13;
答案 0 :(得分:1)
改变这一位:
.menu > li > a:hover {
background-color: #ababab;
}
对此:
.menu > li:hover > a {
background-color: #ababab;
}
他们的关键是,当您将hover
悬停在包含子菜单的<li>
上的任何位置时,您希望* {
padding: 0;
margin: 0;
}
/*main menu*/
.menu {
list-style-type: none;
text-align: center;
text-transform: uppercase;
font-family: "Courier New", Courier, monospace;
background-color: lightgrey;
font-size: 0; /* remove gaps between inline-blocks */
}
/*all links in menu*/
.menu a {
text-decoration: none;
color: #404040;
font-weight: 700;
transition: background-color 0.8s;
}
.menu>li {
display: inline-block;
font-size: 1.5rem; /* change from em to rem */
}
.menu>li>a {
display: block;
padding: 10px 12px;
}
.menu>li:hover>a {
background-color: #ababab;
}
/*second step menus*/
.sStep {
position: absolute;
/*to nice 'fade in'*/
visibility: hidden;
opacity: 0;
transition: visibility 0.5s, opacity 0.5s linear;
list-style-type: none;
background-color: #ababab;
}
.sStep>li {
font-size: 0.75em;
border-bottom: 1px solid #444;
}
.sStep>li:last-child {
border: none;
}
.sStep>li>a {
display: block;
padding: 10px;
}
.sStep>li>a:hover {
background-color: #909090;
}
/*'fade in hover' */
.menu>li:hover>.sStep {
visibility: visible;
opacity: 1;
}
状态受到影响。
演示如下:
<nav>
<ul class="menu">
<li><a href="#">Lorem</a></li>
<li><a href="#">ipsum</a>
<ul class="sStep">
<li><a href="#">Suspendisse</a></li>
<li><a href="#">Donec commodo</a></li>
<li><a href="#">sodales consectetur</a></li>
<li><a href="#">Maecenas sit amet</a></li>
<li><a href="#">Mauris tempor vitae sem</a></li>
<li><a href="#">Curabitur</a></li>
</ul>
</li>
<li><a href="#">dolor</a></li>
<li><a href="#">sit</a></li>
<li><a href="#">amet</a>
<ul class="sStep">
<li><a href="#">ullamcorper cursus</a></li>
<li><a href="#">Cras blandit nunc</a></li>
<li><a href="#">fermentum</a></li>
<li><a href="#">Neque porro quisquam</a></li>
</ul>
</li>
</ul>
</nav>
thread