如何添加Smooth CSS下拉菜单

时间:2013-12-14 06:36:29

标签: html css drop-down-menu

我设法使用CSS创建一个带有下拉菜单(子菜单)的菜单。但我无法为我的编码添加平滑的下拉菜单效果。我尝试添加-moz-transition等等。我不知道在哪里添加这些转换以使我的正常下拉菜单成为平滑的下拉菜单效果。

以下是我的HTML编码

<div id='cssmenu'>
<ul>
   <li class='active'><a href='index.html'><span>Home</span></a></li>
   <li class='has-sub'><a href='#'><span>Products</span></a>
      <ul>
         <li class='has-sub'><a href='#'><span>Product 1</span></a>
            <ul>
               <li><a href='#'><span>Sub Item</span></a></li>
               <li class='last'><a href='#'><span>Sub Item</span></a></li>
            </ul>
         </li>
         <li class='has-sub'><a href='#'><span>Product 2</span></a>
            <ul>
               <li><a href='#'><span>Sub Item</span></a></li>
               <li class='last'><a href='#'><span>Sub Item</span></a></li>
            </ul>
         </li>
      </ul>
   </li>
   <li><a href='#'><span>About</span></a></li>
   <li class='last'><a href='#'><span>Contact</span></a></li>
</ul>
</div>

这是css

<style media="screen" type="text/css">
    #cssmenu ul,
    #cssmenu li,
    #cssmenu span,
    #cssmenu a {
      margin: 0;
      padding: 0;
      position: relative;
    }
    #cssmenu {
      height: 49px;
      border-radius: 5px 5px 0 0;
      -moz-border-radius: 5px 5px 0 0;
      -webkit-border-radius: 5px 5px 0 0;
      background: #fefefe;
      background: -moz-linear-gradient(top, #fefefe 0%, #eee9f0 100%);
      background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #fefefe), color-stop(100%, #eee9f0));
      background: -webkit-linear-gradient(top, #fefefe 0%, #eee9f0 100%);
      background: -o-linear-gradient(top, #fefefe 0%, #eee9f0 100%);
      background: -ms-linear-gradient(top, #fefefe 0%, #eee9f0 100%);
      background: linear-gradient(top, #fefefe 0%, #eee9f0 100%);
      border-bottom: 2px solid #db000b;
      width: auto;
    }
    #cssmenu:after,
    #cssmenu ul:after {
      content: '';
      display: block;
      clear: both;
    }
    #cssmenu a {
      background: #fefefe;
      background: -moz-linear-gradient(top, #fefefe 0%, #ececec 100%);
      background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #fefefe), color-stop(100%, #ececec));
      background: -webkit-linear-gradient(top, #fefefe 0%, #ececec 100%);
      background: -o-linear-gradient(top, #fefefe 0%, #ececec 100%);
      background: -ms-linear-gradient(top, #fefefe 0%, #ececec 100%);
      background: linear-gradient(top, #fefefe 0%, #ececec 100%);
      color: #000;
      display: inline-block;
      font-family: Helvetica, Arial, Verdana, sans-serif;
      font-size: 12px;
      line-height: 49px;
      padding: 0 20px;
      text-decoration: none;
    }
    #cssmenu ul {
      list-style: none;
    }
    #cssmenu > ul {
      float: left;
    }
    #cssmenu > ul > li {
      float: left;
    }
    #cssmenu > ul > li > a {
      color: #000;
      font-size: 12px;
    }
    #cssmenu > ul > li:hover:after {
      content: '';
      display: block;
      width: 0;
      height: 0;
      position: absolute;
      left: 50%;
      bottom: 0;
      border-left: 10px solid transparent;
      border-right: 10px solid transparent;
      border-bottom: 10px solid #db000b;
      margin-left: -10px;
    }
    #cssmenu > ul > li:first-child > a {
      border-radius: 5px 0 0 0;
      -moz-border-radius: 5px 0 0 0;
      -webkit-border-radius: 5px 0 0 0;
    }
    #cssmenu > ul > li.active:after {
      content: '';
      display: block;
      width: 0;
      height: 0;
      position: absolute;
      left: 50%;
      bottom: 0;
      border-left: 10px solid transparent;
      border-right: 10px solid transparent;
      border-bottom: 10px solid #db000b;
      margin-left: -10px;
    }
    #cssmenu > ul > li.active > a {
      -moz-box-shadow: inset 0 0 2px rgba(0, 0, 0, 0.1);
      -webkit-box-shadow: inset 0 0 2px rgba(0, 0, 0, 0.1);
      box-shadow: inset 0 0 2px rgba(0, 0, 0, 0.1);
      background: #ececec;
      background: -moz-linear-gradient(top, #ececec 0%, #ffeeff ef 100%);
      background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #ececec), color-stop(100%, #ffeeff ef));
      background: -webkit-linear-gradient(top, #ececec 0%, #ffeeff ef 100%);
      background: -o-linear-gradient(top, #ececec 0%, #ffeeff ef 100%);
      background: -ms-linear-gradient(top, #ececec 0%, #ffeeff ef 100%);
      background: linear-gradient(top, #ececec 0%, #ffeeff ef 100%);
    }
    #cssmenu > ul > li:hover > a {
      background: #ececec;
      background: -moz-linear-gradient(top, #ececec 0%, #ffeeff ef 100%);
      background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #ececec), color-stop(100%, #ffeeff ef));
      background: -webkit-linear-gradient(top, #ececec 0%, #ffeeff ef 100%);
      background: -o-linear-gradient(top, #ececec 0%, #ffeeff ef 100%);
      background: -ms-linear-gradient(top, #ececec 0%, #ffeeff ef 100%);
      background: linear-gradient(top, #ececec 0%, #ffeeff ef 100%);
      -moz-box-shadow: inset 0 0 2px rgba(0, 0, 0, 0.1);
      -webkit-box-shadow: inset 0 0 2px rgba(0, 0, 0, 0.1);
      box-shadow: inset 0 0 2px rgba(0, 0, 0, 0.1);
    }
    #cssmenu .has-sub {
      z-index: 1;
    }
    #cssmenu .has-sub:hover > ul {
      display: block;
    }
    #cssmenu .has-sub ul {
      display: none;
      position: absolute;
      width: 200px;
      top: 100%;
      left: 0;
    }
    #cssmenu .has-sub ul li {
      *margin-bottom: -1px;
    }
    #cssmenu .has-sub ul li a {
      background: #db000b;
      border-bottom: 1px dotted #ff0f1b;
      filter: none;
      font-size: 11px;
      display: block;
      line-height: 120%;
      padding: 10px;
      color: #ffffff;
    }
    #cssmenu .has-sub ul li:hover a {
      background: #a80008;
    }
    #cssmenu .has-sub .has-sub:hover > ul {
      display: block;
    }
    #cssmenu .has-sub .has-sub ul {
      display: none;
      position: absolute;
      left: 100%;
      top: 0;
    }
    #cssmenu .has-sub .has-sub ul li a {
      background: #a80008;
      border-bottom: 1px dotted #ff0f1b;
    }
    #cssmenu .has-sub .has-sub ul li a:hover {
      background: #8f0007;
    }
    </style>

4 个答案:

答案 0 :(得分:1)

当你写'平滑的CSS菜单'时你的意思是没有javascript而只有CSS?

如果是,请使用css level 3 transition。

#cssmenu ul ul { 
    /* this will apply to inner UL, adapt to your desired selector */
    -webkit-transition: height 0.3s ease-in-out;
    -o-transition: height 0.3s ease-in-out;
    -moz-transition: height 0.3s ease-in-out;
    transition: height 0.3s ease-in-out;
}

<强> [编辑]

如果它不适用于您的上下文,也许是因为没有'内部UL元素的高度。

#cssmenu .has-sub:hover > ul {
  display: block;
  height:auto; /* add this */
}
#cssmenu .has-sub ul {
  display: none;
  position: absolute;
  width: 200px;
  height:0; /* add this */
  top: 100%;
  left: 0;
}

有关跨浏览器解决方案主题的更多信息,请在网上搜索 css转换生成器

答案 1 :(得分:0)

我建议使用更广泛的东西,比如jQuery,来提供你想要的效果。这是“手风琴”效果的链接,听起来像是你想要的东西。手风琴菜单的布局是完全可调的 - 所以如果您不喜欢默认布局的设计,请不要担心。

http://jqueryui.com/accordion/#default

将jQuery添加到您的网页大约需要30秒,但会为您的页面提供永久有用的添加,动画和交互式内容操作!

答案 2 :(得分:0)

你可以使用jquery为你的下拉菜单缓解效果这是一个例子

http://devsnippets.com/article/slick-drop-down-menu-with-easing-effect-using-jquery-css.html

答案 3 :(得分:0)

我用这个 http://cssdeck.com/labs/pure-css3-smooth-drop-down-menu

此菜单仅在CSS3中创建,灵感来自Forrst上的这篇文章!这包括一个嵌套的ul结构来创建菜单的第二级,最初隐藏它的不透明度设置为0.当链接悬停时,相应的ul显示并向下滑动。这可以通过将其不透明度设置为1并在与CSS3过渡一起悬停时更改它的顶部偏移来平滑地为该过程设置动画来实现。

查看代码并尽可能多地使用它:)