我如何使用CSS使我的下拉菜单淡入淡出

时间:2014-12-12 13:25:20

标签: html css



#menu {
  width: 100%;
  margin: 10px auto;
}
#menu ul,
#menu li {
  margin: 0 auto;
  padding: 0 0;
  list-style: none
}
#menu ul {
  width: 100%;
}
#menu li {
  float: left;
  display: inline;
  position: relative;
}
#menu a {
  display: block;
  line-height: 35px;
  padding: 0 14px;
  text-decoration: none;
}
#menu li a {
  border-right: 1px solid #e5e5e5;
}
#menu li a:hover {
  color: #444;
  background-color: #e5e5e5;
}
#menu input {
  display: none;
  margin: 0 0;
  padding: 0 0;
  width: 80px;
  height: 30px;
  opacity: 0;
}
#menu label {
  font-size: 20px;
  display: none;
  width: 35px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  color: #77778b
}
#menu label span {
  position: absolute;
  left: 35px;
}
#menu ul.menus {
  height: auto;
  overflow: hidden;
  width: 180px;
  border-top: 1px solid #e5e5e5;
  border-left: 1px solid #e5e5e5;
  border-bottom: 1px solid #e5e5e5;
  position: absolute;
  z-index: 99;
  display: none;
  left: -1px;
}
#menu ul.menus li {
  display: block;
  width: 100%;
}
#menu ul.menus a {
  color: #77778b;
}
#menu li:hover ul.menus {
  display: block
}

<nav id='menu'>
  <input type='checkbox' />
  <label><i class='icon-reorder' /><span>Menu</span>
  </label>
  <ul>
    <li><a href='/'>Home</a>
    </li>
    <li><a class='dropdown' href='#'>Menu1</a>
      <ul class='menus'>
        <li><a href='#'>Submenu1</a>
        </li>
        <li><a href='#'>Submenu2</a>
        </li>
      </ul>
    </li>
    <li><a class='dropdown' href='#'>Menu2</a>
      <ul class='menus'>
        <li><a href='#'>Submenu1</a>
        </li>
        <li><a href='#'>Submenu2</a>
        </li>
        <li><a href='#'>Submenu3</a>
        </li>
      </ul>
    </li>

  </ul>
</nav>
&#13;
&#13;
&#13;

如何只使用CSS使下拉菜单淡入?有点愚蠢的问题,但仍然试图理解CSS任何人都知道如何使它成为可能plz建议它。 谢谢!

1 个答案:

答案 0 :(得分:3)

一种可能的解决方案是使用opacity: 0代替display: none并在悬停时使用transition

&#13;
&#13;
#menu {
  width: 100%;
  margin: 10px auto;
}
#menu ul,
#menu li {
  margin: 0 auto;
  padding: 0 0;
  list-style: none
}
#menu ul {
  width: 100%;
}
#menu li {
  float: left;
  display: inline;
  position: relative;
}
#menu a {
  display: block;
  line-height: 35px;
  padding: 0 14px;
  text-decoration: none;
}
#menu li a {
  border-right: 1px solid #e5e5e5;
}
#menu li a:hover {
  color: #444;
  background-color: #e5e5e5;
}
#menu input {
  display: none;
  margin: 0 0;
  padding: 0 0;
  width: 80px;
  height: 30px;
  opacity: 0;
}
#menu label {
  font-size: 20px;
  display: block;
  width: 35px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  color: #77778b;
  visibility: hidden;
}
#menu label span {
  position: absolute;
  left: 35px;
}
#menu ul.menus {
  height: auto;
  overflow: hidden;
  width: 180px;
  border-top: 1px solid #e5e5e5;
  border-left: 1px solid #e5e5e5;
  border-bottom: 1px solid #e5e5e5;
  position: absolute;
  z-index: 99;
  display: block;
  left: -1px;
  opacity: 0;
}
#menu ul.menus li {
  display: block;
  width: 100%;
}
#menu ul.menus a {
  color: #77778b;
}
#menu li:hover ul.menus {
  opacity: 1;
  -webkit-transition: opacity 1s ease-in-out;
  -moz-transition: opacity 1s ease-in-out;
  -ms-transition: opacity 1s ease-in-out;
  -o-transition: opacity 1s ease-in-out;
  transition: opacity 1s ease-in-out;
}
&#13;
<nav id='menu'>
  <input type='checkbox' />
  <label><i class='icon-reorder' /><span>Menu</span>
  </label>
  <ul>
    <li><a href='/'>Home</a>
    </li>
    <li><a class='dropdown' href='#'>Menu1</a>
      <ul class='menus'>
        <li><a href='#'>Submenu1</a>
        </li>
        <li><a href='#'>Submenu2</a>
        </li>
      </ul>
    </li>
    <li><a class='dropdown' href='#'>Menu2</a>
      <ul class='menus'>
        <li><a href='#'>Submenu1</a>
        </li>
        <li><a href='#'>Submenu2</a>
        </li>
        <li><a href='#'>Submenu3</a>
        </li>
      </ul>
    </li>

  </ul>
</nav>
&#13;
&#13;
&#13;

此外,您可以使用transition相同的方式为鼠标移出动画:

&#13;
&#13;
#menu {
  width: 100%;
  margin: 10px auto;
}
#menu ul,
#menu li {
  margin: 0 auto;
  padding: 0 0;
  list-style: none
}
#menu ul {
  width: 100%;
}
#menu li {
  float: left;
  display: inline;
  position: relative;
}
#menu a {
  display: block;
  line-height: 35px;
  padding: 0 14px;
  text-decoration: none;
}
#menu li a {
  border-right: 1px solid #e5e5e5;
}
#menu li a:hover {
  color: #444;
  background-color: #e5e5e5;
}
#menu input {
  display: none;
  margin: 0 0;
  padding: 0 0;
  width: 80px;
  height: 30px;
  opacity: 0;
}
#menu label {
  font-size: 20px;
  display: block;
  width: 35px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  color: #77778b;
  visibility: hidden;
}
#menu label span {
  position: absolute;
  left: 35px;
}
#menu ul.menus {
  height: auto;
  overflow: hidden;
  width: 180px;
  border-top: 1px solid #e5e5e5;
  border-left: 1px solid #e5e5e5;
  border-bottom: 1px solid #e5e5e5;
  position: absolute;
  z-index: 99;
  display: block;
  left: -1px;
  opacity: 0;
  -webkit-transition: opacity 0.5s ease-in-out;
  -moz-transition: opacity 0.5s ease-in-out;
  -ms-transition: opacity 0.5s ease-in-out;
  -o-transition: opacity 0.5s ease-in-out;
  transition: opacity 0.5s ease-in-out;
}
#menu ul.menus li {
  display: block;
  width: 100%;
}
#menu ul.menus a {
  color: #77778b;
}
#menu li:hover ul.menus {
  opacity: 1;
  -webkit-transition: opacity 1s ease-in-out;
  -moz-transition: opacity 1s ease-in-out;
  -ms-transition: opacity 1s ease-in-out;
  -o-transition: opacity 1s ease-in-out;
  transition: opacity 1s ease-in-out;
}
&#13;
<nav id='menu'>
  <input type='checkbox' />
  <label><i class='icon-reorder' /><span>Menu</span>
  </label>
  <ul>
    <li><a href='/'>Home</a>
    </li>
    <li><a class='dropdown' href='#'>Menu1</a>
      <ul class='menus'>
        <li><a href='#'>Submenu1</a>
        </li>
        <li><a href='#'>Submenu2</a>
        </li>
      </ul>
    </li>
    <li><a class='dropdown' href='#'>Menu2</a>
      <ul class='menus'>
        <li><a href='#'>Submenu1</a>
        </li>
        <li><a href='#'>Submenu2</a>
        </li>
        <li><a href='#'>Submenu3</a>
        </li>
      </ul>
    </li>

  </ul>
</nav>
&#13;
&#13;
&#13;