0

我的下拉菜单中有问题(这是我的小提琴:http: //jsfiddle.net/iamthestig/Vhs2v/)。当您单击产品时,左侧会出现一个下拉菜单,但底部的其他导航元素也会向下滑动。有没有办法阻止这种情况发生?我已经阅读了几个小时的下拉菜单教程。这就是为什么我有这个代码。但我无法实现我想要发生的事情。希望你们能帮帮我。谢谢!

这是我的代码:

HTML

<body>
<nav>
     <ul class="nav">
         <li><a href="#">Home</a></li>
         <li class="products">
             <a href="#">Products</a>
                 <ul class="subnav-products clearfix">
                     <li><a href="#">Product One</a></li>
                     <li><a href="#">Product Two</a></li>
                     <li><a href="#">Product Three</a></li>
                     <li><a href="#">Product Four</a></li>
                     <li><a href="#">Product Five</a></li>
                     <li><a href="#">Product Six</a></li>
                 </ul>
         </li>
         <li><a href="#">Services</a></li>
         <li><a href="#">About Us</a></li>
         <li><a href="#">Contact Us</a></li>
    </ul>             
</nav>

​</p>

CSS

    * {
    margin: 0;
    padding: 0;
}

body {
    width: 100%;
    height: 100%;
    background: darkgray;
}

.nav {
    width: 100px;
    position: absolute;
    top: 30px;
    right: 20px;
    list-style: none;
}

.nav li {
    margin: 0 0 10px 0;
    font-family: Helvetica;
    font-size: 11px;
    text-align: right;
    text-transform: uppercase;
    line-height: 1;
}

.nav li:nth-child(5) {
    margin: 0;
}

.nav li a {
    padding: 10px 10px 10px 0;
    color: white;
    text-decoration: none;
    background: dimgray;
    display: block;
}

.subnav-products {
    width: 300px;
    position: relative;
    top: -31px;
    left: -300px;
    display: none;
}
.subnav-products li {
    width: 150px;
    margin: 0;
    text-align: center;
    float: left;
}

.subnav-products li a {
    display:block;
}

.clearfix:before,
.clearfix:after {
    content: " "; /* 1 */
    display: table; /* 2 */
}

.clearfix:after {
    clear: both;
}​

JS/JQUERY

$(".products").on("click", function(){
    $(".subnav-products").slideToggle();
    $(this).toggleClass("active");
});​
4

1 回答 1

1

我给了a元素相对定位,然后给了.subnav元素绝对定位。在稍微弄乱了位置之后,这就是我想出的……

http://jsfiddle.net/Vhs2v/2/

CSS

.nav li a {
    padding: 10px 10px 10px 0;
    color: white;
    text-decoration: none;
    background: dimgray;
    display: block;
    position: relative;
}
.subnav-products {
    width: 300px;
    position: absolute;
    top: 41px;
    left: -300px;
    display: none;
}
于 2012-11-23T17:11:58.700 回答