开发者

z-index issue with jquery .show()

开发者 https://www.devze.com 2023-03-15 23:47 出处:网络
I\'m having a maddening problem with jQuery\'s .show() function. I have a navigation bar on top of my webpage.When you hover over the last element in the navbar, a subnav menu should pop up below it.

I'm having a maddening problem with jQuery's .show() function.

I have a navigation bar on top of my webpage. When you hover over the last element in the navbar, a subnav menu should pop up below it. That part works wonderfully.

The jQuery code looks like this:

$('#navbar li.etc').hover(
    function() { 
        $('#subnav').show();
        $('li.etc a.main').addClass('hover');
    },
    function() { 
        $('#subnav').hide(); 
        $('li.etc a.main').removeClass('hover');
    }
);

The HTML code looks like this:

<ul id="navbar">
    <li><a href="#">home</a></li>
    <li><a href="#">about us</a></li>
    <li><a href="#">developers</a></li>
    <li><a href="#">pricing</a></li>
    <li class="etc"><a class="main" href="#">etc &raquo;</a>
    <ul id="subnav">
        <li><a href="#">api</a></li>
        <li><a href="#">contact</a></li>
        <li><a href="#">careers</a></li>
        <li><a href="#">twitter</a></li>
    </ul>
    </li>
</ul>

And the CSS looks like this:

#navbar {
    position: absolute;
    height: 30px;
    /*width: 455px;*/
    right: 85px;
    bottom: 40px;
}

#navbar li {
    float: left;
    list-style: none;
    padding: 2px 5px 12px 5px;
    margin-right: 5px;
    border-radius: 5px;
    border-bottom-right-radius: 0px;
    border-bottom-left-radius: 0px;
    -moz-border-radius: 5px;
    -moz-border-radius-bottomright: 0px;
    -moz-border-radius-bottomleft: 0px;
    border: 1px solid #50A6C8;
}

    #navbar li.etc:hover {
        background: #9DD3E7;
        color: #203C4D;
        border: 1px solid gray;
    }

        #navbar li a {
            margin: 0px auto 0px auto;
            list-style: none;
            /*color: #325E77;*/
            color: white;
            font: 1.8em "Helvetica", "Lucida Grande", Serif;
            text-decoration: none;
        }

        #navbar li a:hover {
            color: #203C4D;

        }

        #navbar li a.hover {
            color: #203C4D;
        }

#subnav {
    display: none;
    position: absolute;
    right: 5px;
    top: 35px;
    width: 340px;
    border-radius: 5px;
    border-top-right-radius: 0px;
    -moz-border-radius: 5px;
    -moz-border-radius-topright: 0px;
    padding: 13px 13px 5px 13px;
    font: .9em "Helvetica", "Lucida Grande", Serif;
    background: #9DD3E7;
    z-index: 1;
    box-shadow: 5px 5px #888;
    -moz-box-shadow: 5px 5px #888;
    -webkit-box-shadow: 5px 5px 5px #888;
    border: 1px solid gray;
}

#subnav li {
    float: left;
    list-style: none;
    margin: 0px auto 0px auto;
    border: none;
}

    #subnav li a {
        padding: 2px 8px 2px 8px;
        text-decoration: none;
        color: #325E77;
    }

    #subnav li a:hover {
            color: #203C4D;
    }

I'm having a problem with the border and z-index. I have a border on the subnav div, and a border on the top and sides of the navbar element. What I would like to happen, is to have the subnav pop up, using .show(), and have it appear UNDER the navbar element, ("etc" in my page), so that it looks like a single seamless piece. But right now, the subnav is popping up ABOVE the last navbar element, so the border shows. Here is an online example:

[link redacted]

I've played a bunch with the z-index properties of each element, but I just can't seem to get it right. I found a number of resources dealing with some kind of problem involving the z-index and IE, but my problem exists in webkit and mozilla browsers. So those resources didn't help me out very much.

Does anyone have a suggest开发者_运维问答ion? I'm tearing my hair out over this! Thanks!


Your problem is happening because subnav is a child element of the the listitem etc. therefore it can never be "below" it, no matter how you play your z-indexes.

What you can try to do, is detach it from being under the <li> and position it absolutely.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号