j查询自定义垂直下拉菜单



我的下拉菜单有问题(这是我的小提琴: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>

.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");
});​

我给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;
}

最新更新