纯CSS下拉触发错误元素的悬停

时间:2014-07-25 22:21:53

标签: html css html5 css3

所以我有一个纯css下拉菜单,但我有一个问题。当您将鼠标悬停在链接上时,下拉列表会起作用,但当您将鼠标悬停在包含下拉部分的实际容器上时,它也会触发。这是代码的jsfiddle。 http://jsfiddle.net/9BRac/

<div class="nav-con">
  <nav role='navigation' id="nav">
    <ul>
      <li id="linked"><a href="#">Home</a></li>
      <li><a href="#">About</a>
          <ul class="dropdown">
            <li>Link 1.</li>
            <li>Link 2.</li>
            <li>Link 3</li>
          </ul>  
      </li>
      <li><a href="#">Clients</a>
          <ul class="dropdown">
            <li>Link 1.</li>
            <li>Link 2.</li>
            <li>Link 3</li>
          </ul>  
      </li>
      <li><a href="#">Contact Us</a>
          <ul class="dropdown">
            <li>Link 1.</li>
            <li>Link 2.</li>
            <li>Link 3</li>
          </ul>  
      </li>
    </ul>
    </nav>


.clearfix:before,  
.clearfix:after {  
    content: " ";  
    display: table;  
}  
.clearfix:after {  
    clear: both;  
}  
.clearfix {  
    *zoom: 1;  
}  

nav {
  width: 100%;
  height: 60px;
  background-color:black;
  border-bottom: #ffd600 solid 10px; 
  margin: 0 auto;
}

nav ul {
  position:inheirt;
}

nav ul li {
    transition: all .6s ease-in-out;
  -moz-transition: all .6s ease-in-out;
  -o-transition: all .6s ease-in-out;
  -webkit-transition: all .6s ease-in-out;
  width: 80px;
  height:60px;
  margin-left:10px;
  display:;
  float: left;
  line-height: 60px;
  text-align:center;
  list-style:none;
  position:inherit;
  color: white;
  text-decoration:none;
}

 nav ul li:hover {
  width: 80px;
  -moz-transition: all .6s ease-in-out;
  -o-transition: all .6s ease-in-out;
  -webkit-transition: all .6s ease-in-out;
  height:60px;
  margin-left:10px;
  background-color:#ffd600;
  float: left;
  line-height: 60px;
  text-align:center;
  list-style:none;
  position:inherit;
  color: white;
  text-decoration:none;
}

nav ul li a {
  color: white;
  text-decoration:none;
}

html {
  background-color: #f2f2f2;
}

#dropdown {
    width:100%;
  height:200px;
  opacity: 0.8;
  background-color:black;
    display:none;
}


.dropdown {
  margin-top:10px;

}
.dropdown li {
  width: 300px;
  background-color: #ffd600;
}

nav ul ul {
    opacity: 0;
  transition: all .6s ease-in-out;
  -moz-transition: all .6s ease-in-out;
  -o-transition: all .6s ease-in-out;
  -webkit-transition: all .6s ease-in
}

nav ul li:hover > ul {
        opacity: 0.8;
    transition: all .6s ease-in-out;
    -moz-transition: all .6s ease-in-out;
    -o-transition: all .6s ease-in-out;
    -webkit-transition: all .6s ease-in-out;
    }

nav ul ul li {
  opacity: 1.0 !important;
}

nav ul ul li:hover {
  width: 350px !important;
}

2 个答案:

答案 0 :(得分:2)

使用以下

/* Your dropdown */
nav ul ul {
    opacity: 0;
    visibility:hidden;
    transition: opacity .6s ease-in-out;
    -moz-transition: opacity .6s ease-in-out;
    -o-transition: opacity .6s ease-in-out;
    -webkit-transition: opacity .6s ease-in
}

/* Code to display your dropdown */
nav ul li:hover > ul {
    opacity: 0.8;
    visibility:visible;
}

有关工作示例,请转到此处http://jsfiddle.net/DanielApt/9BRac/12/

<强>说明: 您的.dropdown仍然存在,只有opacity 0

每当您将鼠标悬停在此“隐身”.dropdown上时,都会触发其父li的悬停样式,从而再次显示.dropdown

解决方案:为visibility设置hidden .dropdown,并且只有当您将鼠标悬停在父visibility上时才将visible设置为li 1}}

同样只使用opacity属性上的转换,然后visibility会立即更改,但opacity会平滑过渡。

PS:如果您对我未使用display:none的原因感到好奇,那是因为您无法转换显示(source for the solution using visibiliy

答案 1 :(得分:0)

尝试使用visibility:hidden,而不是像这样编辑悬停时的不透明度:

http://jsfiddle.net/9BRac/18/

现在,您可以编辑转换时间位以使其更平滑。

同样,过渡属性只能用于不透明度,而不能用于所有属性。