I have two scripts, that I have created. One which is for the desktop multi-level dropdown navigation, and the second which is for when it goes responsive. Is there a way to simplify/compress this script, which I am not seeing?
Here is my simplified version.
$(document).ready(function () {
$('ul.primary li').click(function () {
var a = this;
if ($('ul', this).is(':visible')) {
$('ul', this).slideUp(function () {
$(a).removeClass('active')
})
} else {
$('ul.drop').slideUp();
$('ul.primary li').removeClass('active');
$('ul', this).slideDown();
$(a).addClass('active')
}
});
$('body').click(function (a) {
if (!$(a.target).is('a')) {
$('ul.drop').slideUp();
$('ul.primary li').removeClass('active')
}
});
$(function () {
$('.mobile-nav').click(function (e) {
$('.primary').slideToggle(150, "swing");
e.stopPropagation()
});
$(window).resize(function() {
$('ul.drop').slideUp();
$('ul.primary li').removeClass('active')
});
})
});
Here is the markup:
<div class="nav">
<div class="mobile-nav">Navigation</div>
<ul class="primary">
<li><a href="#link">Link</a></li>
<li><a class="has-drop">Link w/ Children <span></span></a>
<ul class="drop">
<li><a href="#link">Link</a></li>
<li><a href="#link">Link</a></li>
<li><a href="#link">Link</a></li>
<li><a href="#link">Link</a></li>
<li><a href="#link">Link</a></li>
</ul>
</li>
<li><a class="has-drop" href="#link">Link w/ Children <span></span></a>
<ul class="drop">
<li><a href="#link">Link</a></li>
<li><a href="#link">Link</a></li>
<li><a href="#link">Link</a></li>
<li><a href="#link">Link</a></li>
<li><a href="#link">Link</a></li>
</ul>
</li>
<li><a href="#link">Link</a></li>
<li><a href="#link">Link</a></li>
</ul>
<div class="clear"></div>
</div>
</div>
The problem that I am having now, is that the window.Resize function isn’t firing all the time. I am seeking to close all open ul.drop’s when the window is resized.
Also, when resizing my browser back to desktop width, my ul.drops seem to cut-off and will not slideDown past the containing element until i refresh.
http://dean.edwards.name/packer/ is a good place to compress it yourself.
Pretty self explanatory how to use 🙂