Hello fellow stackoverflow users,
I seem to be having some issues trying to get my drop down menu to work and I'm pretty sure it is my javascript that isn't working properly. As of right now, with what I have - works when clicking the <li>
element to open up the <ul>
menu and stays in an active state, however, you have to click the <li>
for it to close and the active state stays on rather than goes inactive like i need it too...So my questions at hand is where am I going wrong? I am still fairly new to javascript and any help would be greatly appreciated.
Requirements:
1.) on click make active and open menu
2.) either on click again on element or anywhere on page for that matter, menu closes and goes inactive.
This is what I have so far: DEMO
HTML:
<div class="top_l">
<li>Nav <span>▼</span>
<ul> <a href="#">Title</a>
<a href="#">Title</a>
<a href="#">Title</a>
</ul>
</li>
</div>
JS:
$(document).ready(function () {
$('.top_l li').click(function () {
$('.top_l li').addClass('active');
$('.top_l li ul').slideToggle();
});
});
CSS:
.top_l {
width: 340px;
height: 60px;
float: left;
}
.top_l li {
height: 32px;
padding: 8px 12px 0 12px;
margin: 10px 0 0 6px;
border: 1px solid transparent;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
float: left;
-moz-transition: all .3s ease;
-webkit-transition: all .3s ease;
-ms-transition: all .3s ease;
transition: all .3s ease;
list-style: none;
color: #A4A4A4;
font: 20px Arial, Helvetica, sans-serif;
font-weight: bold;
font-variant: small-caps;
text-shadow: 2px 2px 3px #000;
position: relative;
cursor: pointer;
}
.top_l li span {
font: 14px Arial, Helvetica, sans-serif;
}
.top_l li:hover, .top_l li.active {
color: #FFF;
border: 1px solid #444;
}
.top_l li ul {
width: 120px;
height: 120px;
background: #222;
border: 1px solid #444;
-moz-box-shadow: inset 0 0.1em 0.4em 0.1em #000;
-webkit-box-shadow: inset 0 0.1em 0.4em 0.1em #000;
box-shadow: inset 0 0.1em 0.4em 0.1em #000;
display: none;
position: absolute;
top: 22px;
left: 4px;
}
.top_l li ul a {
width: 118px;
height: 28px;
padding: 10px 0 0 0;
background: red;
float: left;
color: #FFF;
font: 14px Arial, Helvetica, sans-serif;
font-weight: bold;
font-variant: small-caps;
text-shadow: 1px 1px 1px #000, -2px -2px 2px #000;
}