IAMTHESTIG
IAMTHESTIG

Reputation: 418

jQuery Custom Vertical Drop Down Menu

I have a problem in my drop-down-menu (here's my fiddle: http://jsfiddle.net/iamthestig/Vhs2v/). When you click products there's a drop-down-menu on the left side, but the other navigation elements in the bottom slide down too. Is there a way to stop that from happening? I've been reading some tutorials for the drop-down-menu for a couple of hours. And that's why I have this code. But I can't achieve what I want to happen. Hope you guys can help me out. Thanks!

Here's my code:

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

Upvotes: 0

Views: 1702

Answers (1)

Reinstate Monica Cellio
Reinstate Monica Cellio

Reputation: 26143

I gave the a elements relative positioning, and then the .subnav elements absolute positioning. After a bit of messing with the position this is what I came up with...

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

Upvotes: 1

Related Questions