Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS3 :hover animation has z-index bug

Ok, so I have an empty <span> that is nested in side my <li>'s of an unordered list. The span holds a background image that is supposed to appear on :hover. The problem is that while the animation is transitioning, the z-index is wrong, the <span> stacks itself in front of the <a> element which precedes it in the DOM. As soon as the animation completes, however, the stacking order corrects itself. The result is a visual sudden "snap" of the effect and also the link becomes unclickable for the duration of the CSS3 transition.

Can anyone break down what is happening at the DOM level? How can I fix this?

You can see a working example that demonstrates the issue here: http://jsfiddle.net/qZkfw/1/

My 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>

my 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%;
}
like image 751
Brian Avatar asked Nov 27 '11 05:11

Brian


People also ask

Does Z-index affect hover?

the order of the elements will dictate which hover effect will occur. If you have a z-index on 1 element of 1000 and 999 on the other. does not allow the hover transitions of the element @ 999 to occur.

How do I fix a Z-index problem?

To sum up, most issues with z-index can be solved by following these two guidelines: Check that the elements have their position set and z-index numbers in the correct order. Make sure that you don't have parent elements limiting the z-index level of their children.

Can Z-index be animated?

Yes, you can animate z-index ! It's not visually apparent in this demo, but seeing the interpolated values go from 1 to 5 confirms it.


1 Answers

Add position:relative; z-index: -1; to #nav-main li span.

Updated jsfiddle.

Edit:

I figured it out.

Static elements do not obey z-index, so you need to add position:relative; to #nav-main li a.

Updated jsfiddle.

like image 199
bookcasey Avatar answered Oct 07 '22 04:10

bookcasey