如何使CSS Dropdown菜单滑落

时间:2014-05-17 21:54:17

标签: html css css3

我创建了一个带有下拉列表的CSS菜单,当我将鼠标悬停在菜单项上时,我隐藏了子菜单并显示它,我想知道是否可以使用CSS进行某种类型的CSS过渡来使其向下滑动。你蛋糕看看这里的模拟菜单。



#main-nav {
  position: relative;
}

#main-nav>ul>li {
  width: 12.5%;
  float: left;
  text-align: center;
  line-height: .9em;
  font-weight: bold;
  background: #ccc;
}

#main-nav>ul>li>a {
  display: block;
  color: #333;
  line-height: 12px;
  font-size: 14px;
  padding: 22px 0;
  text-decoration: none;
}

.nav-dropdown {
  margin: -5px auto;
  position: absolute;
  left: -999em;
  /* Hides the drop down */
  text-align: left;
  padding: 0;
  border-top: 0;
  width: 500px;
  background: #333;
  color: #f2f2f2;
  border-bottom: 10px solid #25272a;
  -webkit-border-bottom-right-radius: 5px;
  -webkit-border-bottom-left-radius: 5px;
  -moz-border-radius-bottomright: 5px;
  -moz-border-radius-bottomleft: 5px;
  border-bottom-right-radius: 5px;
  border-bottom-left-radius: 5px;
}

#main-nav li:hover .nav-dropdown {
  left: 0;
  top: auto;
  z-index: 11;
}

<div id="main-nav">
  <ul>
    <li><a href="#">Item 1</a></li>
    <li><a href="#">Item 2</a></li>
    <li><a href="#">Item 3</a>
      <!-- Start Blog Drop Down-->
      <div class="nav-dropdown">
        <p>have this item slide down from CSS</p>
      </div>
      <!-- /.nav-dropdown -->
    </li>
  </ul>
</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

是的,你可以,但你需要使用here所述的max-height属性,或者你可以使用上一个答案中描述的固定高度。

<强> CSS

.nav-dropdown {
    margin: 0 auto;  
    height: 0; /* Hides the drop down */ 
    overflow: hidden;
    text-align:left;  
    padding: 0;  
    border-top: 0;
    width: 500px;
    background:#333;
    color: #f2f2f2;
    -webkit-border-bottom-right-radius: 5px;
    -webkit-border-bottom-left-radius: 5px;
    -moz-border-radius-bottomright: 5px;
    -moz-border-radius-bottomleft: 5px;
    border-bottom-right-radius: 5px;
    border-bottom-left-radius: 5px;
    border-bottom: 0;
    max-height:0px;
}

#main-nav li:hover .nav-dropdown {
    height:auto;
    z-index: 11; 
    max-height:200px;
    transition-property: all;
    transition-duration: 1s;
    border-bottom: 10px solid #25272a;
}

http://jsfiddle.net/L8WVP/7/

This也可能符合您的利益。

答案 1 :(得分:0)

那应该对你有所帮助。只需给出应该首先在高位0中滑动的div标签并进行css转换。

-webkit-transition: height 0.5s ease-in;
-moz-transition: height 0.5s ease-in;
-o-transition: height 0.5s ease-in;
-ms-transition: height 0.5s ease-in;
transition: height 0.5s ease-in;

然后你在悬停部分添加div的高度。

#main-nav li:hover .nav-dropdown {
    left: 0;
    top: auto;
    z-index: 11; 
    height:200px;
}

你完成了。 希望我能帮到你。

http://jsfiddle.net/L8WVP/6/

答案 2 :(得分:0)

<ul class="navigation">
    <li><a href="#">Home</a></li>
    <li class="menu">
    <a href="#">Services</a>
    <ul class="submenu">
      <li><a href="#">Link 1</a></li>
      <li><a href="#">Link 2</a></li>
      <li><a href="#">Link 3</a></li>
      <li><a href="#">Link 4</a></li>
    </ul>
  </li>
  <li><a href="#">About</a></li>
  <li><a href="#">Contact</a></li>
</ul>

    ul {
  padding: 0;
  margin: 0;
  list-style: none;
}
ul li a {
  text-decoration: none;
  color: #e74c3c;
  font-family: Tahoma, Arial;
}
.navigation {
  background-color: a#e74c3c;
  width: 75%;
  margin: 20px auto;
  min-height: 50px;
}
.navigation > li {
  float: left;
  padding: 0 20px;
}
.navigation > li > a {
  line-height: 50px;
  color: #FFF;
}
.navigation .menu {
  position: relative
}
.navigation .menu .submenu {
  position: absolute;
  width: 200px;
  -webkit-box-shadow: 0px 1px 1px #CCC;
  box-shadow: 0px 1px 1px #CCC;
  display: none;
  -webkit-transition: all .5s ease-in-out;
  -moz-transition: all .5s ease-in-out;
  transition: all .5s ease-in-out;
}
.navigation .menu:hover .submenu {
  display: block
}
.navigation .menu .submenu:before {
  content: "";
  position: absolute;
  display: block;
  border-width: 0 6px 6px 6px;
  border-style: solid;
  border-color: #FFF transparent;
  top: -6px;
  left: 5px
}
.navigation .menu .submenu li {
  padding: 5px 10px;
  -webkit-transition: all .5s ease-in-out;
  -moz-transition: all .5s ease-in-out;
  transition: all .5s ease-in-out;
}
.navigation .menu .submenu li:hover {
  background-color: #e74c3c;
}
.navigation .menu .submenu li:hover a {
  color: #FFF;
  padding: 0 11px;
}