2

我有一个使用 CSS 的下拉菜单,现在我被告知他们希望下拉菜单显示得慢一些。一切都很好,但到目前为止,当用户将鼠标悬停在导航上时,我无法减慢转换速度。使用纯 CSS 解决方案执行此操作的最佳方法是什么?

JSFiddle上的演示

HTML

<nav>
  <ul>
    <li><a href="#">Line 1</a></li>
    <li><a href="#">Line 2</a>
      <ul>
        <li><a href="#">SubCat 1</a></li>
        <li><a href="#">SubCat 2</a></li>
        <li><a href="#">SubCat 3</a></li>
      </ul>
    </li>
    <li><a href="#">Line 3</a>
      <ul>
        <li><a href="#">SubCat 4</a></li>
        <li><a href="#">SubCat 5</a></li>
        <li><a href="#">SubCat 6</a></li>
      </ul>
    </li>
    <li><a href="#">Line 4</a>
      <ul>
        <li><a href="#">SubCat 7</a></li>
        <li><a href="#">SubCat 8</a></li>
        <li><a href="#">SubCat 9</a></li>
      </ul>
    </li> 
  </ul>
</nav>

CSS

nav {
    background: url(../images/global/Navigation_Full.png) no-repeat;
    width: 540px;
    height: 54px;
    margin: 0 auto;
    color: #000;
}

nav ul{
    display: inline-table;
    list-style: none;
    width: 100%;
    position: relative;
    border-top: 1px solid #FF9F69;
}

nav ul:after {
    content: ""; clear: both; display: block;
}

nav ul ul {
    display: none;
    position: absolute;
    width: auto;
    top: 100%;
}

nav ul li:hover > ul {
    display: block;
}

nav li:first-child {
    border-left: none;
}

nav li {
    text-align: center;
    margin-left: 1px;
    float: left;
    height: 38px;
    line-height: 40px;
    padding: 0 10px;
}

nav li a {
    padding: 10px 17px;
    font-size: 0.813em;
    text-decoration: none;
    text-shadow: inherit;
    color: #000;
}

nav li:hover {
    box-shadow: 1px 1px 20px 1px rgba(64, 23, 0, 0.4) inset;
    -webkit-transition-property:color, background;
    -moz-transition-property:color, background;
    -webkit-transition-duration: 0.2s;
    -moz-transition-duration: 0.2s;
    -webkit-transition-timing-function: linear, ease-in;
    -moz-transition-timing-function: linear, ease-in;
}

nav ul ul {
    background: rgb(125,56,16); /* Old browsers */
    background: -moz-linear-gradient(top,  rgba(125,56,16,1) 0%, rgba(233,103,31,1) 30px, rgba(233,103,31,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(125,56,16,1)), color-stop(30px,rgba(233,103,31,1)), color-stop(100%,rgba(233,103,31,1))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top,  rgba(125,56,16,1) 0%,rgba(233,103,31,1) 30px,rgba(233,103,31,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top,  rgba(125,56,16,1) 0%,rgba(233,103,31,1) 30px,rgba(233,103,31,1) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top,  rgba(125,56,16,1) 0%,rgba(233,103,31,1) 30px,rgba(233,103,31,1) 100%); /* IE10+ */
    background: linear-gradient(to bottom,  rgba(125,56,16,1) 0%,rgba(233,103,31,1) 30px,rgba(233,103,31,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#7d3810', endColorstr='#e9671f',GradientType=0 ); /* IE6-9 */
    border-radius: 0px; padding: 0;
    border-top: none;
    z-index: 1;
}

nav ul ul li {
    float: inherit; 
    position: relative;
    border-left: none;
    border-right: none;
    text-align: left;
    font-size: 1em;
    font-weight: lighter;
}
    nav ul ul li a {
        color: #fff;
    }   
        nav ul ul li a:hover {
            color: #000;
        }

nav ul ul ul {
    position: absolute; left: 100%; top:0;
}
4

3 回答 3

5

您已经为主菜单设置了动画,但没有为下拉菜单设置动画。我已将下拉菜单中的转换从设置 display:block 更改为将 opacity 设置为 1,

nav ul li:hover > ul {
    display: block;
    opacity: 1;
}

并在基础元素中添加显示块,将不透明度设置为 0,并对其进行动画处理。

nav ul ul {
    background: rgb(125,56,16); /* Old browsers */
    display: block;
    opacity: 0;
    -webkit-transition: opacity 3s;
}

结果是:

http://jsfiddle.net/vals/SmNq3/1/

于 2013-01-14T22:01:02.210 回答
2

CSS3 过渡有一个可以设置的持续时间属性。

#example
{
   transition-property: top;
   transition-duration: 2s;
}

注意:支持可能需要使用供应商前缀(-webkit、-o、-ms、-moz)。

于 2013-01-14T13:55:07.197 回答
0

在您的 CSS 中尝试此代码

div {
    overflow:hidden;
    background:#000;
    color:#fff;
    height:0;
    padding: 0 18px;
    width:100px;
    -webkit-transition: all .5s ease;
    -moz-transition: all .5s ease;
    transition: all .5s ease;
}

.animated {
    height:auto;
    padding: 24px 18px;
}

检查这个http://jsfiddle.net/catharsis/n5XfG/17/

于 2013-01-14T16:02:50.460 回答