开发者

CSS hover menu: getting hovered menu item to keep hovered state css

CSS hover menu: getting hovered menu item to keep hovered state css

CSS hover menu: getting hovered menu item to keep hovered state css

In this situation how would I get the setting to keep the hovered state while going threw t开发者_C百科he menu items? Is there a CSS only way or would I have to introduce some javascript? Thanks.


It is possible with just css.

For example: If your menu is made of nested lists:

li:hover {
    background: #color;
}

li:hover ul {
    display: block;
}

li:hover ul li {}

Fiddle: http://jsfiddle.net/maniator/ZC4xv/

CSS from the above example:

#nav-menu > li {
    background: orange;
    float: left;
    padding: 10px;
    border: 1px solid grey;
}

#nav-menu > li:hover a {
    background: red;
    padding: 2px;
    display: inline;
}

#nav-menu > li ul {
    display: none;
    position: absolute;
}

#nav-menu > li:hover ul {
    display: block;
    margin-left: 5px;
}

#nav-menu > li:hover ul li {
    background: blue;
}

#nav-menu > li:hover ul li:hover {
    background: green;
}


Assuming the sub-menu is a nested list, you can use jQuery to add a hover class to the parent menu item:

<ul class="primary-nav">  
    <li>Menu Item 1
        <ul class="sub-nav">
            <li>Sub-Menu Item 1</li>
            <li>Sub-Menu Item 2</li>
        </ul>
    </li>
    <li>Menu Item 2</li>
</ul>

<script>
    $('.primary-nav li').hover(function(){
        $(this).addClass('hover');
    }, function(){
        $(this).removeClass('hover');
    });
</script>  


Try with top level item inside a span tag. Assuming:

<li>
    <a><span>Setting</span></a>
    <ul>
    ...
    </ul>
</li>

Then this is for CSS:

li:hover span {
    background: #color;
    display: block;
}

li:hover ul {
    display: block;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜