CSS3:悬停动画有z-index错误

时间:2011-11-27 05:10:41

标签: css debugging user-interface css3

好的,所以我有一个空的<span>嵌套在无序列表的<li>侧。该范围包含应该​​出现在:hover上的背景图像。问题在于,当动画正在转换时,z-index是错误的,<span>将自身堆叠在DOM之前的<a>元素前面。但是,一旦动画完成,堆叠顺序就会自行更正。结果是效果的视觉突然“快照”,并且链接在CSS3过渡期间变得不可点击。

有人可以分解DOM级别发生的事情吗?我该如何解决这个问题?

您可以在此处查看演示此问题的实际示例:http://jsfiddle.net/qZkfw/1/

我的HTML

        <div id="nav">
          <ul id="nav-main">
            <li><a href="#">Home</a><span></span></li>
            <li><a href="#">About</a><span></span></li>
            <li><a href="#">Get Fit</a><span></span>
                <ul class="nav-secondary">
                  <li><a href="#">Exercise Library</a></li>
                  <li><a href="#">Find An Instructor</a></li>
                  <li><a href="#">Fitness Tools</a></li>
                </ul>
            </li>
            <li><a href="#">Find An Instructor</a><span></span></li>
            <li><a href="#">Get Certified</a><span></span></li>
          </ul>
        </div>

我的CSS

#nav-main li {
    margin:0;
    padding:5px;
    position: relative;
    display: block;
    float: left;
    margin-left: 10px;
}

#nav-main li a {
    color: #97dd6e;
    font-size: 1.1em;
    text-decoration: none;
    text-align: center;
    margin-right: -7px;
    line-height: 19px;
    z-index: 99;
}

#nav-main li span {
    height: 28px;
    background: transparent url('/images/application/bg_nav_active_repeat.png') repeat-x top center;
    display: block;
    margin: -22px -5px 0 2px;
    padding: 3px 0 0 0;
    z-index: 98;

    opacity: 0;
    visibility: hidden;     
    -webkit-transition: all 0.2s ease-in-out;
    -moz-transition: all 0.2s ease-in-out;
    -o-transition: all 0.2s ease-in-out;
    transition: all 0.2s ease-in-out;   
}
#nav-main li:hover span {
    opacity: 1;
    visibility: visible;
    -webkit-transition: all 0.4s ease-in-out;
    -moz-transition: all 0.4s ease-in-out;
    -o-transition: all 0.4s ease-in-out;
    transition: all 0.4s ease-in-out;   

}
#nav-main li span:before, #nav-main li span:after {
    content: '';
    height: 28px;
    width: 7px;
    display: block;
    background: transparent url('/images/application/bg_nav_active_before.png') no-repeat left top;
    position: absolute;
    top: 2px;
    left: 0;
    z-index: 999;
}
#nav-main li span:after {
    background: transparent url('/images/application/bg_nav_active_after.png') no-repeat right top;
    left: 100%;
}

1 个答案:

答案 0 :(得分:6)

position:relative; z-index: -1;添加到#nav-main li span

<德尔> Updated jsfiddle

修改

我明白了。

静态元素不服从z-index,因此您需要将position:relative;添加到#nav-main li a

Updated jsfiddle