下拉菜单上的淡入效果并不总是发生

时间:2015-12-22 11:52:02

标签: html css drop-down-menu opacity fade

我希望我的下拉菜单在鼠标悬停时具有淡入效果。我已经为此编写了以下代码,但有时只能观察到淡入效果,而不是总是如此。代码:



* {
  margin: 0;
  padding: 0;
}

.header {
  position: fixed;
  list-style-type: none;
  height: 35px;
  width: 100%;
  background: #333333;
}

.header > li {
  display: inline-block;
  padding: 8px;
  position: relative;
  color: #FFF;
}

.header > li:hover {
  background: #000000;
}

.dropdown ul {
  list-style-type: none;
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  color: red;
  width: 100%;
  opacity: 0;
  background: yellow;
  border: 1px solid black;
  transition: opacity 1s;
}

.dropdown:hover ul {
  opacity: 1;
  display: block;
}

<ul class="header">
  <li class="home">Test</li>
  <li class="dropdown">Dropdown &#10097;
    <ul>
      <li>item 1</li>
      <li>item 2</li>
      <li>item 3</li>
    </ul>
  </li>
</ul>
&#13;
&#13;
&#13;

我认为当我取消并将鼠标悬停在&#34; Dropdown❱&#34;时,效果不会发生。一秒钟之内。我试图仅使用HTML和CSS来做到这一点。

我如何实现我的目标?

顺便说一句,如何为菜单创建淡出效果?

2 个答案:

答案 0 :(得分:0)

display不是动画属性,因此您可以使用visibility代替display

以下是animatable-properties

的列表

* {
  margin: 0;
  padding: 0;
}
.header {
  position: fixed;
  list-style-type: none;
  height: 35px;
  width: 100%;
  background: #333333;
}
.header > li {
  display: inline-block;
  padding: 8px;
  position: relative;
  color: #FFF;
}
.header > li:hover {
  background: #000000;
}
.dropdown ul {
  list-style-type: none;
  /*display: none;*/
  visibility: hidden;
  position: absolute;
  top: 100%;
  left: 0;
  color: red;
  width: 100%;
  opacity: 0;
  background: yellow;
  border: 1px solid black;
  transition: visibility 1s, opacity 1s;
}
.dropdown:hover ul {
  opacity: 1;
  visibility: visible;
}
<ul class="header">
  <li class="home">Test</li>
  <li class="dropdown">Dropdown &#10097;
    <ul>
      <li>item 1</li>
      <li>item 2</li>
      <li>item 3</li>
    </ul>
  </li>
</ul>

答案 1 :(得分:0)

你可以用一点jQuery来实现这个目标......

 resultArray = []
 rLength = 0.0
 rCount = len(subQueryArray)
 rLength = len(subQueryArray)/29.0
 arrayLength = int(math.ceil( rLength ))
 # If subqueries are greater than 30 than divide sub-query length by 29 or 30
 if arrayLength > 1:
     for ii in range (0, arrayLength):
 #srange = start range, nrange = new range
 if ii == 0:
     srange = ii
 else:
     srange = nrange + 1
 nrange = 29 * (ii + 1)
 newList = []
 for nii in range (srange, nrange+1):
 if nii < rCount:
     newList.append(subQueryArray[nii])
     query = db.GqlQuery(“SELECT * FROM table_name ” +“WHERE req_id in:1”,newList)
 for result in query.run():
 # result.id belongs to table entity
 resultArray.append(result.id)

JSFIDDLE链接:https://jsfiddle.net/omerblink/7n59kew1/

如果您仍然希望将其保留为HTML / CSS,那么只需放弃显示和使用可见性的想法。

$('.header ul').hide();
$('.header li').hover(function(){
        $(this).find('ul').fadeIn();
}, function(){
        $(this).find('ul').fadeOut();
});

JSFIDDLE链接到HTML / CSS解决方案:https://jsfiddle.net/omerblink/jwd9hLo1/